android之sqlite简单使用

本文详细介绍如何在Android应用中使用SQLiteOpenHelper创建数据库,并通过实例演示数据库的基本操作,包括创建表、插入数据、更新数据及查询数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,创建SqLitDBHelper类,继承android.database.sqlite.SQLiteOpenHelper,重载onCreate()

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package  tools;
import  android.content.Context;
import  android.database.sqlite.SQLiteDatabase;
import  android.database.sqlite.SQLiteDatabase.CursorFactory;
import  android.database.sqlite.SQLiteOpenHelper;
public  class  SqLitDBHelper  extends  SQLiteOpenHelper {
  public  SqLitDBHelper(Context context, String name, CursorFactory factory,
    int  version) {
   super (context, name, factory, version);
   // TODO Auto-generated constructor stub
         
  }
 
  @Override
  public  void  onCreate(SQLiteDatabase arg0) { //Called when the database is created for the first time
   // TODO Auto-generated method stub
   arg0.execSQL( "create table userinfo(userId long,userName varchar(25))" );
  }
  @Override
  public  void  onUpgrade(SQLiteDatabase arg0,  int  arg1,  int  arg2) {
   // TODO Auto-generated method stub
  }
}

2,对数据库进行操作,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package  test.activity.sqlite;
import  tools.BackgroundMusicPlay;
import  tools.SqLitDBHelper;
import  android.app.Activity;
import  android.content.ContentValues;
import  android.database.Cursor;
import  android.database.sqlite.SQLiteDatabase;
import  android.os.Bundle;
import  android.view.Menu;
import  android.view.View;
import  android.view.View.OnClickListener;
import  android.widget.Button;
import  android.widget.Toast;
import  com.sl.androidgametest.R;
public  class  MySqliteTest  extends  Activity  implements  OnClickListener {
  private  Button btn_sqlCreat; // 创建数据库
  private  Button btn_sqlInsert; // 插入数据
  private  Button btn_sqlUpdate; // 更新数据
  private  Button btn_sqlQuerry; // 查询数据
  SqLitDBHelper dbh; //声明SqLitDBHelper
  @Override
  protected  void  onCreate(Bundle savedInstanceState) {
   super .onCreate(savedInstanceState);
   setContentView(R.layout.activity_next);
   //创建数据库:tsetDb1
   dbh=  new  SqLitDBHelper(MySqliteTest. this "tsetDb1" , null 1 );
   btn_sqlCreat = (Button) findViewById(R.id.sqlCreate);
   btn_sqlInsert = (Button) findViewById(R.id.sqlInsert);
   btn_sqlUpdate = (Button) findViewById(R.id.sqlUpdate);
   btn_sqlQuerry = (Button) findViewById(R.id.sqlQuerry);
   btn_sqlCreat.setOnClickListener( this );
   btn_sqlInsert.setOnClickListener( this );
   btn_sqlUpdate.setOnClickListener( this );
   btn_sqlQuerry.setOnClickListener( this );
  }
  @Override
  public  void  onClick(View arg0) {
   switch  (arg0.getId()) {
   case  R.id.sqlCreate:
//Create and/or open a database that will be used for reading and writing.  
    SQLiteDatabase sqldb = dbh.getWritableDatabase(); //
    System.out.println( "create or open database success!" );
    break ;
   case  R.id.sqlInsert:
    ContentValues ctv =  new  ContentValues();  // 得到ContentValues对象
    ctv.put( "userId" 1 );  // 放入键值对,键要与列名一致,值要与列的数据类型一致
    ctv.put( "userName" "小名" );  // 放入键值对,键要与列名一致,值要与列的数据类型一致
    SQLiteDatabase sqldb1 = dbh.getWritableDatabase(); // 得到一个SQLiteDatabase对象,用于操控数据库
    sqldb1.insert( "userinfo" null , ctv); // 增加数据库记录
    System.out.println( "success insert a new content!" );
    break ;
   case  R.id.sqlUpdate:
    ContentValues ctv1 =  new  ContentValues();
    ctv1.put( "userName" "小明" );
    SQLiteDatabase sqldb2 = dbh.getWritableDatabase(); // 得到一个SQLiteDatabase对象,用于操控数据库
    try  {
     sqldb2.update( "userinfo" , ctv1,  "userId=?" ,
       new  String[] {  "1"  }); // 更新数据库记录
    catch  (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    System.out.println( "success updata the content!" );
    break ;
   case  R.id.sqlQuerry:
    SQLiteDatabase sqldb3 = dbh.getWritableDatabase(); // 得到一个SQLiteDatabase对象,用于操控数据库
    Cursor cursor;
    try  {
     cursor = sqldb3.query( "userinfo" new  String[] {  "userId" ,
       "userName"  },  "userId=?" new  String[] {  "1"  },  null ,
       null null );
     while  (cursor.moveToNext()) {  // 打印输出
      String name = cursor.getString(cursor
        .getColumnIndex( "userName" ));
      System.out.println( "query result:-->neme="  + name);
     }
    catch  (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    break ;
   }
  }
}

3,关于SQLiteOpenHelper的其他方法可查看api文档http://tool.oschina.net/apidocs/apidoc?api=android/reference

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值