常量类:ConstantUtil
public class ConstantUtil {// 数据库名称
public static final String DATABASE_NAME = "user_manager.db"
// 数据库版本号
public static final int DATABASE_VERSION = 1
//表名
public static final String TABLE_NAME = "user_info"
//字段名
public static final String USER_ID = "userId"
public static final String USER_NAME = "username"
public static final String USER_PASSWORD = "password"
public static final String USER_ADDRESS = "address"
}
自定义SQLiteOpenHelper:MySQLiteOpenHelper
public class MySQLiteOpenHelper extends SQLiteOpenHelper {// 定义一个SQLiteDatabase对象,对表进行相应的 *** 作
private SQLiteDatabase mDatabase
public MySQLiteOpenHelper(Context context) {
super(context, ConstantUtil.DATABASE_NAME, null,
ConstantUtil.DATABASE_VERSION)
mDatabase = getWritableDatabase()
}
/*
* 创建表
*/
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//sql语句
String sql = "create table " + ConstantUtil.TABLE_NAME + "("
+ ConstantUtil.USER_ID + " integer primary key,"
+ ConstantUtil.USER_NAME + " text not null,"
+ ConstantUtil.USER_PASSWORD + " text not null,"
+ ConstantUtil.USER_ADDRESS + " text not null)"
db.execSQL(sql)
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
onCreate(db)
}
/**
* 添加数据
* @param cv
* @return
*/
public boolean insertData(ContentValues cv){
return mDatabase.insert(ConstantUtil.TABLE_NAME, null, cv)>0
}
/**
* 查询所有数据
* @return
*/
public List<Userinfo> queryData(){
List<Userinfo> userinfos=new ArrayList<Userinfo>()
//从数据库里查询数据
Cursor cursor=mDatabase.query(ConstantUtil.TABLE_NAME, null, null, null, null, null, null)
if(cursor!=null){
//取出数据
while (cursor.moveToNext()) {
Userinfo userinfo=new Userinfo()
userinfo.setUserId(cursor.getInt(0))
userinfo.setUsername(cursor.getString(1))
userinfo.setPassword(cursor.getString(2))
userinfo.setAddress(cursor.getString(3))
userinfos.add(userinfo)
}
}
return userinfos
}
}
主Activity
public class MainActivity extends Activity {// 控件
private TextView txtName, txtPwd, txtAddress
private EditText edtName, edtPwd, edtAddress
private ListView mListView
// 数据库对象
private MySQLiteOpenHelper mySQLiteOpenHelper
private UserinfoAdapter adapter
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
findView()
init()
}
private void findView() {
// TODO Auto-generated method stub
edtName = (EditText) findViewById(R.id.id_edit1)
edtPwd = (EditText) findViewById(R.id.id_edit2)
edtAddress = (EditText) findViewById(R.id.id_edit3)
mListView = (ListView) findViewById(R.id.id_listview)
}
private void init() {
// TODO Auto-generated method stub
mySQLiteOpenHelper = new MySQLiteOpenHelper(MainActivity.this)
}
public void onAction(View v) {
switch (v.getId()) {
case R.id.id_btn_add:
//添加数据
String userName=edtName.getText().toString()
String userPwd=edtPwd.getText().toString()
String userAdress=edtAddress.getText().toString()
//传入参数
ContentValues cv=new ContentValues()
//列名和值
cv.put(ConstantUtil.USER_NAME, userName)
cv.put(ConstantUtil.USER_PASSWORD, userPwd)
cv.put(ConstantUtil.USER_ADDRESS, userAdress)
//得到结果
boolean flag=mySQLiteOpenHelper.insertData(cv)
if (flag) {
Toast.makeText(MainActivity.this, "添加记录成功", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(MainActivity.this, "添加记录失败", Toast.LENGTH_SHORT).show()
}
break
case R.id.id_btn_query:
//查询数据
List<Userinfo> userinfos=mySQLiteOpenHelper.queryData()
if (adapter!=null) {
adapter=null
}
adapter=new UserinfoAdapter(userinfos)
mListView.setAdapter(adapter)
break
default:
break
}
}
//数据适配器
class UserinfoAdapter extends BaseAdapter{
List<Userinfo> userinfos
public UserinfoAdapter(List<Userinfo> _userinfos){
this.userinfos=_userinfos
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return userinfos.size()
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return userinfos.get(position)
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
convertView=LayoutInflater.from(MainActivity.this).inflate(R.layout.listview_item, null)
txtName=(TextView) convertView.findViewById(R.id.id_txt_name)
txtPwd=(TextView) convertView.findViewById(R.id.id_txt_pwd)
txtAddress=(TextView) convertView.findViewById(R.id.id_txt_address)
txtName.setText(userinfos.get(position).getUsername())
txtPwd.setText(userinfos.get(position).getPassword())
txtAddress.setText(userinfos.get(position).getAddress())
}
return convertView
}
}
}
完整源码下载地址(附数据库文件查询软件+运行效果图):
区别如下:
1、数据库方面,两者注解映射不同
xUtils2下 @Table(name="stu",execAfterTableCreated ="" ),而xUtils3下直接是代替了onCreated。
2、数据库创建和查询有改动,但基本还是相同
xutils2中db.findFirst(Selector.from(Parent.class)。where.....而xUtils3中则是db.selector(Parent.class).where(....).findFirst();但是里面基本的条件例如where,whereBuilder等参数还是没变的。
3、网络 *** 作方面不同
xUtils3是直接x.image().bind..或者x.http().post()等取代了http.send(method,url,callback)。
4、视图方面不同
xutils3中@Event()代替了@onClick(),而且下面的private代替了public。
添加表字段(如果一次添加多字段后面不加bit) ALTER TABLE 表名 ADD 字段名 bit 例如: alter table main add 年龄char(3),姓名varchar(8),性别char(2)欢迎分享,转载请注明来源:内存溢出
评论列表(0条)