Android SQLite数据操作

main.xml文件:

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:orientation="vertical"
04     android:layout_width="fill_parent"
05     android:layout_height="fill_parent"
06     >
07 <Button 
08     android:layout_width="fill_parent"
09     android:layout_height="wrap_content"
10     android:id="@+id/create"
11     android:text="创建数据库"
12     />
13 <Button 
14     android:layout_width="fill_parent"
15     android:layout_height="wrap_content"
16     android:id="@+id/upgrade"
17     android:text="更新数据库"
18     />
19 <Button 
20     android:layout_width="fill_parent"
21     android:layout_height="wrap_content"
22     android:id="@+id/insert"
23     android:text="插入数据"
24     />
25 <Button 
26     android:layout_width="fill_parent"
27     android:layout_height="wrap_content"
28     android:id="@+id/update"
29     android:text="修改数据"
30     />
31 <Button 
32     android:layout_width="fill_parent"
33     android:layout_height="wrap_content"
34     android:id="@+id/select"
35     android:text="查询数据"
36     />
37 </LinearLayout>

继承自SQLiteOpenHelper的MyOpenHleper类: 
01 package com.android.danny.hleper;
02  
03 import android.content.Context;
04 import android.database.sqlite.SQLiteDatabase;
05 import android.database.sqlite.SQLiteOpenHelper;
06  
07 public class MyOpenHleper extends SQLiteOpenHelper {
08  
09     public static final int VERSION =1;
10      
11     public MyOpenHleper(Context context, String name) {
12         super(context, name, null, VERSION);
13         // TODO Auto-generated constructor stub
14     }
15      
16     public MyOpenHleper(Context context, String name,int ver) {
17         super(context, name, null, ver);
18         // TODO Auto-generated constructor stub
19     }
20     @Override
21     public void onCreate(SQLiteDatabase db) {
22         System.out.println("------Create Database----");
23         db.execSQL("CREATE TABLE person (id int ,name varchar(20))");
24     }
25  
26     @Override
27     public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
28         // TODO Auto-generated method stub
29         System.out.println("------Upgrade Database----");
30     }
31  
32 }

SQLite数据操作类: 
01 package com.android.danny.db;
02  
03 import com.android.danny.hleper.MyOpenHleper;
04  
05 import android.app.Activity;
06 import android.content.ContentValues;
07 import android.database.Cursor;
08 import android.database.sqlite.SQLiteDatabase;
09 import android.os.Bundle;
10 import android.view.View;
11 import android.view.View.OnClickListener;
12 import android.widget.Button;
13  
14 public class Sqlite3 extends Activity implements OnClickListener{
15      
16     Button create;
17     Button upgrade;
18     Button insert;
19     Button update;
20     Button select;
21      
22     @Override
23     public void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.main);
26          
27         create = (Button)findViewById(R.id.create);
28         upgrade = (Button)findViewById(R.id.upgrade);
29         insert = (Button)findViewById(R.id.insert);
30         update = (Button)findViewById(R.id.update);
31         select = (Button)findViewById(R.id.select);
32          
33         create.setOnClickListener(this);
34         upgrade.setOnClickListener(this);
35         insert.setOnClickListener(this);
36         update.setOnClickListener(this);
37         select.setOnClickListener(this);
38     }
39  
40     @Override
41     public void onClick(View v) {
42         MyOpenHleper dbDatabase;
43         if (v == create) {
44             dbDatabase = new MyOpenHleper(this"test_db.db");
45             //创建数据库
46             dbDatabase.getWritableDatabase();
47              
48         }else if (v == upgrade) {
49             //更新数据库
50             dbDatabase = new MyOpenHleper(this"test_db.db",2);
51             dbDatabase.getReadableDatabase();
52              
53         }else if (v == insert) {
54             //插入数据
55             ContentValues values = new ContentValues();
56             values.put("id"1);
57             values.put("name""danny");
58             dbDatabase = new MyOpenHleper(this"test_db.db");
59             SQLiteDatabase database = dbDatabase.getWritableDatabase();
60             database.insert("person"null, values);
61              
62         }else if (v == update) {
63             //修改数据
64             ContentValues values = new ContentValues();
65             values.put("name""danny&kiki");
66             dbDatabase = new MyOpenHleper(this"test_db.db");
67             SQLiteDatabase database = dbDatabase.getWritableDatabase();
68             database.update("person", values, "id=?"new String[] {"1"});
69              
70         }else if (v == select) {
71             //数据查询操作
72             dbDatabase = new MyOpenHleper(this"test_db.db");
73             SQLiteDatabase database = dbDatabase.getReadableDatabase();
74             Cursor cursor = database.query("person"new String[] {"id","name"},
75                     "id=?"new String[] {"1"},
76                     nullnullnull);
77             while (cursor.moveToNext()) {
78                 String name = cursor.getString(cursor.getColumnIndex("name"));
79                 System.out.println("query:----->"+name);
80             }
81         }
82          
83     }
84 }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值