SQLite CRUD操作代码实例:

1:首先创建一个继承了SQLiteOpenHelper类的MyDatabaseHelper类。实现他的onCreate(SQLiteDatabase db)

onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)方法。

package dataBase.databasetest;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast; public class MyDatabaseHelper extends SQLiteOpenHelper {
private Context mContext; public MyDatabaseHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
mContext=context;
} public static final String CREATE_BOOK="create table book(" //创建book表
+"id integer primary key autoincrement,"
+"author text,"
+"price real,"
+"pages integer,"
+"name text)";
public static final String CREATE_CATEGORY="create table category(" //创建category表
+"id integer primary key autoincrement,"
+"category_name text,"
+"category_code integer)";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO 自动生成的方法存根
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
Toast.makeText(mContext, "创建数据库成功", Toast.LENGTH_LONG).show();
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO 自动生成的方法存根
db.execSQL("drop table if exists book"); //如果已有这两个表。先删除,
db.execSQL("drop table if exists category"); //在调用onCreate()分别创建
onCreate(db);
} }

2.在MainActivity中通过几个按钮事件测试对数据的CRUD:

 package dataBase.databasetest;

 import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity {
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//修改参数为2,执行onUpgrade()方法跟新数据库
dbHelper=new MyDatabaseHelper(this, "BookStor.db", null, 2); //实现构造函数,传入参数,第一个为context。第二个为数据库名称
//创建表
Button create=(Button)this.findViewById(R.id.creat);
create.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
dbHelper.getWritableDatabase();
}
}); //增加数据
Button add=(Button)this.findViewById(R.id.add);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
//开始组装数据
values.put("name", "Android第一行代码");
values.put("author", "郭霖");
values.put("price",66.78);
values.put("pages",400 );
//写入数据
db.insert("book", null, values);
values.clear();
//准备再次写入数据
values.put("name","java讲义");
values.put("author", "张三");
values.put("price", 66.76);
values.put("pages", 789);
db.insert("book", null, values);
values.clear();
Toast.makeText(getApplicationContext(), "数据写入成功", Toast.LENGTH_LONG).show();
}
}); //更新数据
Button updata=(Button)this.findViewById(R.id.updata);
updata.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("price", 100.00);
db.update("book", values, "name=?",new String[]{"平凡的世界"});
values.clear();
Toast.makeText(getApplicationContext(), "数据更新成功", Toast.LENGTH_SHORT).show(); }
}); //删除数据
Button delete =(Button)this.findViewById(R.id.delete);
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
db.delete("book", "id>?", new String[]{"23"}); //第一个参数为表名,二三个参数为限制条件
Toast.makeText(getApplicationContext(), "数据删除成功", Toast.LENGTH_SHORT).show();
}
}); //查询数据
Button find =(Button)this.findViewById(R.id.find);
find.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
Cursor cursor=db.query("book",null, null, null, null, null, null);
if(cursor.moveToFirst()){
do {
String name=cursor.getString(cursor.getColumnIndex("name"));
String author=cursor.getString(cursor.getColumnIndex("author"));
int pages=cursor.getInt(cursor.getColumnIndex("pages"));
double price=cursor.getDouble(cursor.getColumnIndex("price"));
Log.d("MainActivity", name);
Log.d("MainActivity", author);
Log.d("MainActivity", String.valueOf(pages));
Log.d("MainActivity", String.valueOf(price));
} while (cursor.moveToNext());
}
cursor.close();
Toast.makeText(getApplicationContext(), "数据查找完毕",Toast.LENGTH_SHORT).show();
}
}); //事物
Button transAction=(Button)this.findViewById(R.id.transaction);
transAction.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SQLiteDatabase db=dbHelper.getWritableDatabase();
db.beginTransaction();//开启事务
try {
ContentValues values=new ContentValues();
db.delete("book", "id=?", new String[]{"11"}); //删除
values.put("name", "newnewnew");
values.put("price",111);
values.put("author", "牛人");
values.put("pages", 222);
db.update("book", values, "name=?", new String[]{"Android第一行代码"});
Toast.makeText(getApplicationContext(), "事务执行完毕", Toast.LENGTH_SHORT).show();
values.clear();
} catch (Exception e) {
// TODO: handle exception
}finally{
db.endTransaction();//关闭事务
}
}
});
}
}

3:查看数据库数据可以通过在dos中运行 adb shell 或者 SQLite Expert软件查看。

最新文章

  1. Java特性之多态父类与子类之间的调用
  2. UISearchBar控件-让我们来搞定!(转)
  3. NFS网络文件共享
  4. 关于iOS构建版本提交iTunes后,一直不出现,没加号的解决方案
  5. PHP+Nginx环境搭配
  6. const 与 readonly 知多少
  7. 【Shell脚本学习8】Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数
  8. nginx.conf配置
  9. 批处理 Mysql Findstr
  10. tablelayoutpanel内部组件变形
  11. redux深入理解之中间件(middleware)
  12. day13_Mysql事务与数据库连接池学习笔记
  13. Hadoop源码分析(1):HDFS读写过程解析
  14. C#-事件(十八)
  15. ansible 自动化管理
  16. 20180830xlVBA_合并计算
  17. Blazor
  18. 5分钟搭建 nginx +php --------------(LNMP)新手专用
  19. ubuntu中查看各种设备和资源的命令汇总
  20. ios开发之--仿购物类详情页面数量添加小功能

热门文章

  1. TypeScript学习笔记(四):函数
  2. UVa 112 Tree Summing
  3. 如何让自己的电脑发布ASP http://jingyan.baidu.com/article/19192ad853224ce53f570748.html
  4. const 成员方法
  5. Android多媒体数据库之MediaStore研究
  6. Swift3.0相对于2.3语法的一些变化
  7. SparkStreamingTest.scala
  8. 最简单的Java调用C/C++代码的步骤
  9. IE7下position:relative的问题
  10. STOMP协议规范--转载