1、首先需要创建一个类来继承SQLiteOpenHelper去实现其中的方法
public class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(Context context ) {
/*super参数介绍:
* context:上下文
* name:数据库名字
* factory:目的是创建cursor对象
* version:数据库的版本,从1开始
* */
super(context, "ithema.db", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
2、在MainActivity中实例化MyOpenHelper的实例
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getApplicationContext()返回一个上下文对象
MyOpenHelper myOpenHelper = new MyOpenHelper(getApplicationContext());
//getWritableDatabase()打开或创建数据库,如果是第一次就是创建
SQLiteDatabase sqLiteDatabase = myOpenHelper.getWritableDatabase();
/*getReadableDatabase()打开或创建数据库,如果是第一次就是创建
*(和getWritableDatabase()返回的是同一数据库)
* 如果磁盘满了 返回只读的
*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
3、在MyOpenHelper类的onCreate方法中创建表
/*Called when the database is created for the first time.
* 当数据库第一次创建时调用,这个方法特别适合作表结构的初始化 创建表就是写sql语句
* */
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//id一般以下划线开头 sqlite不区分数据类型。底层全部是以string类型存储的
db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20))" );
}
4、在MyOpenHelper类的onUpgrade方法中对数据库版本更新
/*Called when the database needs to be upgraded.
* 当数据库的版本需要升级(即构造函数中的super方法的version参数被修改时)的时候调用
* 这个方法适合做表结构的更新
* */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("alter table info add phone varchar(20)");
}
5、用sql语句执行增删改查
这里,先在mainactivity_xml中 布局四个按钮,每个按钮都有要触发的点击事件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/bt_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Click_1"
android:text="add" />
<Button
android:id="@+id/bt_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Click_2"
android:text="delete" />
<Button
android:id="@+id/bt_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Click_3"
android:text="change" />
<Button
android:id="@+id/bt_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Click_4"
android:text="find" />
</LinearLayout>
然后在MainActivity中修改为:
public class MainActivity extends Activity {
private MyOpenHelper myOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myOpenHelper = new MyOpenHelper(getApplicationContext());
// 打开或创建数据库,如果是第一次就是创建
// SQLiteDatabase sqLiteDatabase = myOpenHelper.getWritableDatabase();
// 打开或创建数据库,如果是第一次就是创建 如果磁盘满了 返回只读的
}
//增
public void Click_1(View v) {
//【1】获取数据库对象
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
//【2】执行增加一条的sql语句
db.execSQL("insert into info(name,phone) values(?,?)", new Object[]{"张三","110"});
//【3】关闭数据库
db.close();
}
//删
public void Click_2(View v) {
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
db.execSQL("delete from info where name=? ", new Object[]{"张三"});
db.close();
}
//更新
public void Click_3(View v) {
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
db.execSQL("update info set phone=? where name=?", new Object[]{"333","张三"});
db.close();
}
//查找
public void Click_4(View v) {
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
Cursor cursor=db.rawQuery("select * from info", null);
if(cursor != null && cursor.getCount()>0){
while(cursor.moveToNext()){
//getString(columnIndex) columnIndex代表列的索引
String name=cursor.getString(1);
String phone=cursor.getString(2);
System.out.println("name:"+name+"phone:"+phone);
}
}
db.close();
}
}
最后,效果就是,点击不同的按钮,能够实现对数据库的增删改查
然而,运用sql语句进行增删改查具有一些缺点:①sql语句容易写错;②执行sql语句没返回值,不好判断是否执行成功
6、用Google封装的API对数据库增删改查
只需要修改MainActivity.java,分别调用insert、delete、update、query方法:
public class MainActivity extends Activity {
private MyOpenHelper myOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myOpenHelper = new MyOpenHelper(getApplicationContext());
// 打开或创建数据库,如果是第一次就是创建
// SQLiteDatabase sqLiteDatabase = myOpenHelper.getWritableDatabase();
// 打开或创建数据库,如果是第一次就是创建 如果磁盘满了 返回只读的
}
//增
public void Click_1(View v) {
//【1】获取数据库对象
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
//【2】执行增加一条的sql语句
// db.execSQL("insert into info(name,phone) values(?,?)", new Object[]{"张三","110"});
//【3】关闭数据库
/*table:表名
*
* ContentValues内部封装了一个map key:对应列名 value:列的值
* */
ContentValues values = new ContentValues();
values.put("name", "王五");
values.put("phone", "123");
long row_id=db.insert("info", null, values);//返回值代表插入新行的id
db.close();
if(row_id>0){
Toast.makeText(this, "添加成功", 1).show();
}
}
//删
public void Click_2(View v) {
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
//db.execSQL("delete from info where name=? ", new Object[]{"张三"});
//返回值代表受影响的行数
long row_affected = db.delete("info", "name=?", new String[]{"王五"});
db.close();
if(row_affected>0){
Toast.makeText(this, "删除了"+row_affected+"行", 1).show();
}
}
//更新
public void Click_3(View v) {
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
//db.execSQL("update info set phone=? where name=?", new Object[]{"333","张三"});
ContentValues values = new ContentValues();
values.put("phone", "000");
//返回值代表受影响的行数
long row_affected = db.update("info", values, "name=?", new String[]{"王五"});
db.close();
if(row_affected>0){
Toast.makeText(this, "更新了"+row_affected+"行", 1).show();
}
}
//查找
public void Click_4(View v) {
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
Cursor cursor = db.query("info", new String[]{"phone"}, "name=?", new String[]{"王五"}, null, null, null);
if(cursor != null&& cursor.getCount()>0){
while(cursor.moveToNext()){
String phone = cursor.getString(0);
System.out.println( ",phone:"+phone);
}
}
db.close();
}
}
然而,这种方法也有缺点:只能单表查询,不能实现多表查询