Android学习之SQLite基础_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > Android学习之SQLite基础

Android学习之SQLite基础

 2014/12/26 23:23:13  zhouhb  程序员俱乐部  我要评论(0)
  • 摘要:1、新建MySQLiteHelper类继承自SQLiteOpenHelperpublicclassMySQLiteHelperextendsSQLiteOpenHelper{privateContextcontext;publicMySQLiteHelper(Contextcontext,Stringname,CursorFactoryfactory,intversion){super(context,name,factory,version);//TODOAuto
  • 标签:android 学习 SQL

1、新建MySQLiteHelper类继承自SQLiteOpenHelper 

public class MySQLiteHelper extends SQLiteOpenHelper {
private Context context;
public MySQLiteHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
this.context=context;
}

public static final String createContact = "create table contact("
+ "id integer primary key autoincrement,"
+ "name text,phone text,email text)";

@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
arg0.execSQL(createContact);

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}

}

2、实现增加、修改、删除、查询

public class MainActivity extends Activity {
MySQLiteHelper mySQLiteHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//调用getWritableDatabase()方法,检测到当前程序中并没有contact.db数据库,于是会创建该数据库并调用MySQLiteHelper中的onCreate方法创建数据表
mySQLiteHelper=new MySQLiteHelper(this, "contact.db", null, 1);
mySQLiteHelper.getWritableDatabase();
Button btnInsert=(Button)findViewById(R.id.btnInsert);
btnInsert.setOnClickListener(new OnClickListener() {
//增加
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SQLiteDatabase db=mySQLiteHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name", "aa");
values.put("phone", "13989999099");
db.insert("contact",null, values);
values.clear();

}
});

Button btnUpdate=(Button)findViewById(R.id.btnUpdate);
btnUpdate.setOnClickListener(new OnClickListener() {
//修改
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SQLiteDatabase db=mySQLiteHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("email", "itzhb@163.com");
db.update("contact", values, "name=?",new String[]{"aa"});
values.clear();
}
});

Button btnDelete=(Button)findViewById(R.id.btnDelete);
btnDelete.setOnClickListener(new OnClickListener() {
//删除
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SQLiteDatabase db=mySQLiteHelper.getWritableDatabase();
//db.delete("contact", "id>?",new String[]{"1"});

//直接使用SQL操作数据库
String sqlString="delete from contact where id=(select max(id) from contact)";
db.execSQL(sqlString);
}
});

Button btnQuery=(Button)findViewById(R.id.btnQuery);
btnQuery.setOnClickListener(new OnClickListener() {
//查询
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SQLiteDatabase db=mySQLiteHelper.getWritableDatabase();

//直接使用SQL查询
Cursor cursor=db.rawQuery("select * from contact where id>?", new String[]{"1"});
if(cursor.moveToFirst()){
do {
int id=cursor.getInt(cursor.getColumnIndex("id"));
String nameString=cursor.getString(cursor.getColumnIndex("name"));
String phoneString=cursor.getString(cursor.getColumnIndex("phone"));
String emailString=cursor.getString(cursor.getColumnIndex("email"));
Log.d("contact","id is "+id);
Log.d("contact","name is "+nameString);
Log.d("contact","phone is "+phoneString);
Log.d("contact","email is "+emailString);
} while (cursor.moveToNext());
}

}
});
}

}

另外可以通过sdk\platform-tools目录下的adb工具使用sqlite3命令管理SQLite数据库。

 

上一篇: Android客户端单线程下载 下一篇: 没有下一篇了!
发表评论
用户名: 匿名