使用特定方法操作SQLite数据库

如果开发者对于SQL语法不熟悉,Android的SQLiteDatabase提供了增、删、改、查语句来操作数据库,虽然Android提供了这些所谓的“便捷”方法操作数据库,但笔者认为这些方法纯属“鸡肋”,毕竟SQL语法是程序员的基本功。不过Android既然提供了这些方法,这里也简单介绍一下。

1、insert :方法签名: long insert(String table, String nullColumnHack, ContentValues values)

table:表名。

nullColumnHack:强行插入null值的数据列的列名。当values为null或它包含的键值对的数量为0时,就起作用了。它不应是主键列的列名,也不应是非空列的列名。

values:代表一行记录的数据。ContentValues类似于Map集合,存放键值对,键为数据列的列名。

2、update:方法签名: int update(String table, ContentValues values, String whereClause, String[] whereArgs)

table:表名。

values:想更新的数据。

whereClause:条件。

whereArgs:为whereClause子句传入参数。即用来替代占位符的内容。

返回的整数是受此update语句影响的记录的条数。

3、delete:方法签名: int  delete(String table, String whereClause,String[] whereArgs)

table:表名

whereClause:条件,满足此条件的记录将会被删除。

whereArgs:用于为whereClause子句传入参数。即替代占位符。

返回的整数是受此delete语句影响的记录的条数。

4、query:方法签名:Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)

table:表名

columns:要查询的列名

selection:查询条件子句 相当于where关键字后面的部分。

selectionArgs:为selection传入参数,替代占位符。

groupBy:控制分组,相当于select语句group by后面的部分

having:用于对分组进行过滤,相当于select语句having后面的部分

orderBy:排序。相当于select语句order by后面的部分

下面通过一个简单实例来演示它们的使用,代码如下:

Activity:

[java]  view plain copy
  1. package com.lovo.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ContentValues;  
  5. import android.database.Cursor;  
  6. import android.database.sqlite.SQLiteDatabase;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.TextView;  
  10. import com.lovo.dao.DBUtil;  
  11. import com.lovo.databasetest.R;  
  12.   
  13. public class DatabaseTestActivity extends Activity {  
  14.     private TextView show;  
  15.     private SQLiteDatabase db;  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.         show = (TextView) findViewById(R.id.main_tv_show);  
  22.         db = DBUtil.getInstance(this);  
  23.     }  
  24.   
  25.     public void click(View v) {  
  26.         switch (v.getId()) {  
  27.   
  28.         case R.id.main_btn_insert:  
  29.             // 添加指定数据  
  30.             ContentValues insertValues = new ContentValues();  
  31.             insertValues.put("s_name""李四");  
  32.             insertValues.put("s_age"23);  
  33.             insertValues.put("s_sex""男");  
  34.             db.insert("t_stu"null, insertValues);  
  35.             break;  
  36.         case R.id.main_btn_delete:  
  37.             // 根据指定条件删除数据  
  38.             db.delete("t_stu""s_name like ?"new String[] { "李_" });  
  39.             break;  
  40.         case R.id.main_btn_update:  
  41.             // 根据指定ID修改数据  
  42.             ContentValues updateValues = new ContentValues();  
  43.             updateValues.put("s_name""新人名");  
  44.             db.update("t_stu", updateValues, "_id=?"new String[] { "2" });  
  45.             break;  
  46.         case R.id.main_btn_find:  
  47.             // 查询所有数据  
  48.             Cursor cursor = db.query("t_stu"nullnullnullnullnull,  
  49.                     null);  
  50.             // 根据指定条件查询  
  51.             // Cursor cursor=db.query("t_stu", null, "s_name like ?", new  
  52.             // String[]{"张%"},null, null, null);  
  53.             StringBuffer sb = new StringBuffer();  
  54.             while (cursor.moveToNext()) {  
  55.                 int id = cursor.getInt(cursor.getColumnIndex("_id"));  
  56.                 String name = cursor.getString(cursor.getColumnIndex("s_name"));  
  57.                 String sex = cursor.getString(cursor.getColumnIndex("s_sex"));  
  58.                 int age = cursor.getInt(cursor.getColumnIndex("s_age"));  
  59.                 sb.append(id + " " + name + " " + sex + " " + age + "\n");  
  60.             }  
  61.             show.setText(sb.toString());  
  62.             break;  
  63.   
  64.         }  
  65.     }  
  66.   
  67. }  

SQLiteOpenHelper子类(DBUtil):

[java]  view plain copy
  1. package com.lovo.dao;  
  2.   
  3. import android.content.Context;  
  4. import android.database.sqlite.SQLiteDatabase;  
  5. import android.database.sqlite.SQLiteDatabase.CursorFactory;  
  6. import android.database.sqlite.SQLiteOpenHelper;  
  7.   
  8. public class DBUtil extends SQLiteOpenHelper {  
  9.     private static DBUtil dbUtil;  
  10.   
  11.     private DBUtil(Context context, String name, CursorFactory factory,  
  12.             int version) {  
  13.         super(context, name, factory, version);  
  14.     }  
  15.   
  16.     public static SQLiteDatabase getInstance(Context context) {  
  17.         if (dbUtil == null) {  
  18.             // 指定数据库名为student,需修改时在此修改;此处使用默认工厂;指定版本为1  
  19.             dbUtil = new DBUtil(context, "student"null1);  
  20.         }  
  21.         return dbUtil.getReadableDatabase();  
  22.     }  
  23.   
  24.     @Override  
  25.     public void onCreate(SQLiteDatabase db) {  
  26.         try {  
  27.             db.execSQL("create table t_stu(_id integer primary key,s_name text,s_age integer,s_sex text)");  
  28.         } catch (Exception e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.     }  
  32.   
  33.     @Override  
  34.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  35.         System.out.println("-----onUpgrade Called-----" + oldVersion + "--->"  
  36.                 + newVersion);  
  37.     }  
  38.   
  39. }  

布局XML:

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical" >  
  5.   
  6.   
  7.     <Button  
  8.         android:id="@+id/main_btn_insert"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:onClick="click"  
  12.         android:text="添加" />  
  13.   
  14.     <Button  
  15.         android:id="@+id/main_btn_delete"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:onClick="click"  
  19.         android:text="删除" />  
  20.   
  21.     <Button  
  22.         android:id="@+id/main_btn_update"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:onClick="click"  
  26.         android:text="修改" />  
  27.   
  28.     <Button  
  29.         android:id="@+id/main_btn_find"  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:onClick="click"  
  33.         android:text="查找" />  
  34.   
  35.     <TextView  
  36.         android:id="@+id/main_tv_show"  
  37.         android:layout_width="wrap_content"  
  38.         android:layout_height="wrap_content" />  
  39.   
  40. </LinearLayout>  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值