您的位置:首页 > 数据库

SQLite数据库的增删改查

2015-12-25 15:05 393 查看
今天学习数据库的增删改查

调用 SQLiteOpenHelper的 getReadableDatabase()或 getWritableDatabase()方法都会返回一个SQLiteDatabase对象,

借助这个对象就可以对数据进行 CRUD 操作了。

简述:

一、数据库添加

SQLiteDatabase中提供了一个 insert()方法,这个方法就是专门用于添加数据的。

db.insert(table, nullColumnHack, values) ;

它接收三个参数,

1)、第一个参数是表名。

2)、第二个参数用于在未指定添加数据的情况下给某些可为空的列自动赋值 NULL, 一般我们用不到这个功能, 直接传入 null 即可。

3)、第三个参数是一个 ContentValues 对象, 它提供了一系列的 put()方法重载,用于向 ContentValues 中添加数据,只需要将表中的每个列名以及相应的待添加数据传入即可。

二、数据库更新

SQLiteDatabase 中提供了一个 update()方法用于对数据进行更新,

db.update(table, values, whereClause, whereArgs) ;

这个方法接收四个参数,

1)、第一个参数是表名;

2)、第二个参数是 ContentValues 对象,要把更新数据在这里组装进去。

3)、第三、第四个参数用于去约束更新某一行或某几行中的数据,不指定的话默认就是更新所有行。

三、删除数据库数据

SQLiteDatabase 中提供了一个 delete()方法用于删除数据,

db.delete(table, whereClause, whereArgs) ;

这个方法接收三个参数,

1)、第一个参数是表名;

2)、第二、第三个参数是约束删除某一行或某几行的数据,不指定的话默认就是删除所有行。

四、查询数据库数据

SQLiteDatabase中提供了一个query()方法用于对数据进行查询。

db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy) ;

这个方法需要传入七个参数。

1)、第一个参数是表名,

2)、第二个参数用于指定去查询哪几列,如果不指定则默认查询所有列。

3)、第三、第四个参数用于去约束查询某一行或某几行的数据,不指定则默认是查询所有行的数据。

4)、第五个参数用于指定需要去 group by 的列,不指定则表示不对查询结果进行 group by 操作。

5)、第六个参数用于对 group by 之后的数据进行进一步的过滤,不指定则表示不进行过滤。

6)、第七个参数用于指定查询结果的排序方式,不指定则表示使用默认的排序方式。

效果图:



代码:

MainActivity.java中代码:

public class MainActivity extends Activity implements OnClickListener {

private MySQLiteOpenHelper helper;
private SQLiteDatabase db;
private ContentValues values;
private Button create_sqliteDataBase;
private Button add_data;
private Button update_data;
private Button del_data;
private Button query_data;
private TextView show_datas;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 实例化MySQLiteOpenHelper,指定数据库名:Book.db,和版本号 1
* 如果此次执行的版本号,比上次的版本号大(如,上次是1
* .这次是2),则执行MySQLiteOpenHelper类中的重写的onUpgrade()方法
*/
helper = new MySQLiteOpenHelper(this, "Book.db", null, 1);
create_sqliteDataBase = (Button) findViewById(R.id.create_sqliteDataBase);
create_sqliteDataBase.setOnClickListener(this);
add_data = (Button) findViewById(R.id.add_data);
add_data.setOnClickListener(this);
update_data = (Button) findViewById(R.id.update_data);
update_data.setOnClickListener(this);
del_data = (Button) findViewById(R.id.del_data);
del_data.setOnClickListener(this);
query_data = (Button) findViewById(R.id.query_data);
query_data.setOnClickListener(this);
show_datas = (TextView) findViewById(R.id.show_datas);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.create_sqliteDataBase:
/**
* 当第一次点击 按钮时,会检测 到当前程序中并没有 Book.db这个数据库, 于是会创建该数据库并调用
* MySQLiteOpenHelper中的 onCreate()方法,这样 Book表也就得到了创建。 再次点击
* 按钮时,会发现此时已经存在 BookStore.db 数据库了,因此不会重复创建一次。
*/
helper.getWritableDatabase();
break;
case R.id.add_data:
db = helper.getWritableDatabase();
values = new ContentValues();
// 开始添加第一组数据
values.put("name", "财报分析");
values.put("author", "张三");
values.put("pages", 300);
values.put("price", 40);
db.insert("book", null, values); // 插入第一条数据
values.clear();
// 开始添加第二组数据
values.put("name", "低风险炒股");
values.put("author", "李四");
values.put("pages", 350);
values.put("price", 39);
long r = db.insert("book", null, values); // 插入第二条数据,r是返回的主键id
Toast.makeText(this, "数据插入完毕,插入数据主键id是:" + r, 1).show();
break;
case R.id.update_data:
db = helper.getWritableDatabase();
values = new ContentValues();
values.put("price", 10);
int d = db.update("book", values, " name = ?",
new String[] { "低风险炒股" });
Toast.makeText(this, "数据更新完毕,更新数据条数: " + d, 1).show();
break;
case R.id.del_data:
db = helper.getWritableDatabase();
int del = db.delete("book", "name = ?", new String[] { "低风险炒股" });
Toast.makeText(this, "数据删除完毕,删除数据条数: " + del, 1).show();
break;
case R.id.query_data:
db = helper.getWritableDatabase();
Cursor cursor = db
.query("book", null, null, null, null, null, null);
StringBuffer sb = new StringBuffer();
int i = 1 ;
if (cursor.moveToFirst()) {
do {
// 遍历cursor对象,取出数据
String name = cursor.getString(cursor
.getColumnIndex("name"));
String author = cursor.getString(cursor
.getColumnIndex("author"));
String pages = cursor.getString(cursor
.getColumnIndex("pages"));
String price = cursor.getString(cursor
.getColumnIndex("price"));
sb.append("数据 "+(i++)) ;
sb.append("--name:" + name + ", author:" + author
+ ", pages:" + pages + ", price :" + price);
sb.append("\n") ;
} while (cursor.moveToNext());
}
show_datas.setText(sb.toString());
cursor.close();
break;
default:
break;
}
}

}


MySQLiteOpenHelper.java中的代码:

public class MySQLiteOpenHelper extends SQLiteOpenHelper {

/**
* 创建数据库表book语句 解析: 1)integer 表示整型, 2)real 表示浮点型, 3)text 表示文本类型, 4)blob
* 表示二进制类型。 5)primary key 将 id 列设为主键,并用 autoincrement关键字表示 id 列是自增长的。
*/
public static final String CREATE_BOOK = "create table book ("
+ "id integer primary key autoincrement, " + "author text, "
+ "price real, " + "pages integer, " + "name text)";

/**
* 创建数据库表Category 语句
*/
public static final String CREATE_CATEGORY = "create table Category ("
+ "id integer primary key autoincrement, " + "category_name text, "
+ "category_code integer)";

/**
* 上下文对象
*/
private Context context;

/**
* 重写SQLiteOpenHelper的构造方法,这个构造方法中接收四个参数:
*
* @param context
*            (上下文 Context,有它才能对数据库进行操作)
* @param name
*            (是数据库名,创建数据库时的名称)
* @param factory
*            (查询数据的时候返回一个自定义的 Cursor,一般都是传入 null)
* @param version
*            (当前数据库的版本号,可用于对数据库进行升级操作)
*/
public MySQLiteOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
this.context = context;
}

/**
* 实例化SQLiteOpenHelper类之后 ,调用getReadableDatabase()或 getWritableDatabase()方法。
* 如果是要创建的数据库不存在,就执行oncreate()方法,创建数据库; 如果已经存在要创建的数据库,则不执行oncreate()方法
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);// 执行sql语句
Toast.makeText(context, "数据库创建成功", 1).show();
}

/**
* 对数据库进行升级,注意: switch 中每一个 case的最后都是没有使用 break ,
* 这是为了保证在跨版本升级的时候,每一次的数据库修改都能被全部执行到
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
case 1:
db.execSQL(CREATE_CATEGORY);
Toast.makeText(context, "更新数据库,旧版本是" + oldVersion, 1).show();
case 2:
Toast.makeText(context, "更新数据库,旧版本是" + oldVersion, 1).show();
default:
}
}

}


下载地址:

SQLite数据库的增删改查下载地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: