当前位置:数据库 > SQLite >>

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       &

补充:移动开发 , Android ,
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,