SQLite数据库简单的认识
SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它
占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多
程序语言相结合,比如
Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源世界著名的数据库管理系统来讲,它的处理速度
比他们都快。SQLite第一个Alpha诞生于2000年5月. 至今已经有10个年头,SQLite也迎来了一个版本 SQLite 3已经发布。
SQLite特性
下面是访问SQLite官方网站: http://www.sqlite. org/ 时第一眼看到关于SQLite的特性.
1. ACID事务
2. 零配置 – 无需安装和管理配置
3. 储存在单一磁盘文件中的一个完整的数据库
4. 数据库文件可以在不同字节顺序的机器间自由的共享
5. 支持数据库大小至2TB
6. 足够小, 大致3万行C代码, 250K
7. 比一些流行的数据库在大部分普通数据库操作要快
8. 简单, 轻松的API
9. 包含TCL绑定, 同时通过Wrapper支持其他语言的绑定
10. 良好注释的源代码, 并且有着90%以上的测试覆盖率
11. 独立: 没有额外依赖
12. Source完全的Open, 你可以用于任何用途, 包括出售它
13. 支持多种开发语言,C, PHP, Perl, Java, ASP .NET,Python
下面我以一个完整的Demo例子来展示对SQLite数据库操作,包括对数据库表的增、删、改、查等基本操作。下面的一个截图是该演示Demo的项目框架图:
通过上面的截图可以看到该项目src目录下包含两个类:MainActivity.java 和 MySQLiteOpenHelper.java 。其中MySQLiteOpenHelper.java是对数据库操作辅助类。
布局文件main.xml的代码: 01 | xml version = "1.0" encoding = "utf-8" ?>
|
02 | < LinearLayout xmlns:android = "http://schemas./apk/res/android" |
03 | android:orientation = "vertical" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "fill_parent" > |
06 | < TextView android:id = "@+id/tv_title" |
07 | android:layout_width = "fill_parent" android:layout_height = "wrap_content" |
08 | android:text = "Hello, Welcome to Andy's blog!" android:textSize = "16sp" /> |
10 | < Button android:id = "@+id/newTable" android:layout_width = "fill_parent" |
11 | android:layout_height = "wrap_content" android:text = "1.新建数据表" /> |
12 | < Button android:id = "@+id/addOne" android:layout_width = "fill_parent" |
13 | android:layout_height = "wrap_content" android:text = "2.插入一条记录" /> |
14 | < Button android:id = "@+id/query" android:layout_width = "fill_parent" |
15 | android:layout_height = "wrap_content" android:text = "3.查询数据库" /> |
16 | < Button android:id = "@+id/editOne" android:layout_width = "fill_parent" |
17 | android:layout_height = "wrap_content" android:text = "4.修改一条记录" /> |
18 | < Button android:id = "@+id/deleteOne" android:layout_width = "fill_parent" |
19 | android:layout_height = "wrap_content" android:text = "5.删除一条记录" /> |
20 | < Button android:id = "@+id/deleteTable" android:layout_width = "fill_parent" |
21 | android:layout_height = "wrap_content" android:text = "6.删除数据表" /> |
23 | < TextView android:id = "@+id/tv_result" |
24 | android:layout_width = "fill_parent" android:layout_height = "wrap_content" |
25 | android:text = "测试显示的结果" android:textSize = "16sp" /> |
MainActivity.java代码:001 | package com.andyidea.sqlite; |
003 | import java.io.IOException; |
004 | import android.app.Activity; |
005 | import android.content.ContentValues; |
006 | import android.database.Cursor; |
007 | import android.database.sqlite.SQLiteDatabase; |
008 | import android.os.Bundle; |
009 | import android.view.View; |
010 | import android.view.View.OnClickListener; |
011 | import android.widget.Button; |
012 | import android.widget.TextView; |
013 | import android.widget.Toast; |
014 | public class MainActivity extends Activity { |
015 | private Button btn_newTable,btn_addOne,btn_query, |
016 | btn_editOne,btn_deleteOne,btn_deleteTable; |
018 | private MySQLiteOpenHelper myOpenHelper; |
019 | private SQLiteDatabase sqlitedb; |
021 | //----以下两个成员变量是针对在SD卡中存储数据库文件使用---- |
022 | //private File path = new File("/sdcard/dbfile"); //数据库文件目录 |
023 | //private File f = new File("/sdcard/dbfile/AndyDemo.db"); //数据库文件 |
025 | /** Called when the activity is first created. */ |
027 | public void onCreate(Bundle savedInstanceState) { |
028 | super.onCreate(savedInstanceState); |
029 | setContentView(R.layout.main); |
034 | myOpenHelper = new MySQLiteOpenHelper( this ); |
036 | //----如要在SD卡中创建数据库文件,先做如下的判断和创建相对应的目录和文件---- |
037 | //if(!path.exists()){ //判断目录是否存在 |
038 | // path.mkdirs(); //创建目录 |
040 | //if(!f.exists()){ //判断文件是否存在 |
042 | // f.createNewFile(); //创建文件 |
043 | // }catch(IOException e){ |
044 | // e.printStackTrace(); |
052 | private void initializeViews(){ |
053 | tv = (TextView)findViewById(R.id.tv_result); |
054 | btn_newTable = (Button)findViewById(R.id.newTable); |
055 | btn_addOne = (Button)findViewById(R.id.addOne); |
056 | btn_query = (Button)findViewById(R.id.query); |
057 | btn_editOne = (Button)findViewById(R.id.editOne); |
058 | btn_deleteOne = (Button)findViewById(R.id.deleteOne); |
059 | btn_deleteTable = (Button)findViewById(R.id.deleteTable); |
061 | btn_newTable.setOnClickListener( new ClickEvent()); |
062 | btn_addOne.setOnClickListener( new ClickEvent()); |
063 | btn_query.setOnClickListener( new ClickEvent()); |
064 | btn_editOne.setOnClickListener( new ClickEvent()); |
065 | btn_deleteOne.setOnClickListener( new ClickEvent()); |
066 | btn_deleteTable.setOnClickListener( new ClickEvent()); |
069 | class ClickEvent implements OnClickListener{ |
071 | public void onClick(View v) { |
073 | //[1]--如果是在默认的路径下创建数据库,那么实例化sqlitedb的操作如下 |
074 | sqlitedb = myOpenHelper.getWritableDatabase(); //实例化数据库 |
076 | //[2]--如果是在SD卡中创建数据库,那么实例化sqlitedb的操作如下 |
077 | //sqlitedb = SQLiteDatabase.openOrCreateDatabase(f, null); |
079 | if (v == btn_newTable){ //1.新建数据表 |
080 | String TABLE_NAME = "andy" ; |
082 | String TEXT = "text" ; |
083 | String str_sql2 = "CREATE TABLE " + TABLE_NAME + "(" + ID |
084 | + " INTEGER PRIMARY KEY AUTOINCREMENT," + TEXT |
086 | sqlitedb.execSQL(str_sql2); |
087 | tv.setText( "新建数据表成功!" ); |
089 | } else if (v == btn_addOne){ //2.插入一条记录 |
091 | //ContentValues是一个哈希表HashMap,key值是对应数据表中字段名称, |
092 | //value值是字段的值。可以通过ContentValues的put方法把数据存放到 |
093 | //ContentValues对象中,然后把数据插入到相对应的数据表中。 |
094 | ContentValues cv = new ContentValues(); |
095 | cv.put(MySQLiteOpenHelper.TEXT, "新数据" ); |
096 | sqlitedb.insert(MySQLiteOpenHelper.TABLE_NAME, null, cv); |
097 | //db.insert(String table, String nullColumnHack, ContentValues values);方法解说 |
098 | //public long insert (String table, String nullColumnHack, ContentValues values) |
100 | //[1]参数table:需要插入操作的表名 |
101 | //[2]参数nullColumnHack:默认null即可,若在插入一行数据时,若没有指定某列的值, |
106 | // String INSERT_DATA = |
107 | // "INSERT INTO andy (id,text) values (1, '第2种插入数据的方法')"; |
108 | // sqlitedb.execSQL(INSERT_DATA); |
110 | tv.setText( "添加新数据成功!" ); |
112 | } else if (v == btn_query){ //3.查询数据库 |
113 | Cursor cur = sqlitedb.rawQuery( "SELECT * FROM " +MySQLiteOpenHelper.TABLE_NAME, null); |
117 | while (cur.moveToNext()){ |
118 | temp += cur.getString(0); //0代表数据列的第一列,即id |
119 | temp += cur.getString(1); //1代表数据列的第二列,即text |
121 | temp += "/n" ; //定义显示数据的格式,一行一个数据 |
126 | } else if (v == btn_editOne){ //4.修改一条记录 |
128 | ContentValues cv = new ContentValues(); |
129 | cv.put(MySQLiteOpenHelper.TEXT, "更新后的数据" ); |
130 | sqlitedb.update( "andy" , cv, "id " + "=" + Integer.toString(1), null); |
132 | //[2]参数values:ContentValues |
133 | //[3]参数whereClause:更新的条件 |
134 | //[4]参数whereArgs:更新条件对应的值 |
137 | // String UPDATA_DATA = |
138 | // "UPDATE andy SET text='通过SQL语句来修改数据' WHERE id=1"; |
139 | // sqlitedb.execSQL(UPDATA_DATA); |
140 | tv.setText( "修改数据成功!" ); |
142 | } else if (v == btn_deleteOne){ //5.删除一条记录 |
144 | sqlitedb. delete ( "andy" , MySQLiteOpenHelper.ID + "= 1" , null); |
145 | //public int delete(String table, String whereClause, String[] whereArgs)解说 |
147 | //[2]参数whereClause:删除的条件 |
148 | //[3]参数whereArgs:删除条件对应的值 |
151 | //String DELETE_DATA = "DELETE FROM andy WHERE id=1"; |
152 | //sqlitedb.execSQL(DELETE_DATA); |
153 | tv.setText( "删除数据成功!" ); |
155 | } else if (v == btn_deleteTable){ //6.删除数据表 |
156 | sqlitedb.execSQL( "DROP TABLE andy" ); |
157 | tv.setText( "删除数据表成功!" ); |
MySQLiteOpenHelper辅助器类代码: 01 | package com.andyidea.sqlite; |
02 | import android.content.Context; |
03 | import android.database.sqlite.SQLiteDatabase; |
04 | import android.database.sqlite.SQLiteOpenHelper; |
05 | import android.util.Log; |
07 | * 此类继承了SQLiteOpenHelper抽象类,是一个辅助器类,需要 |
11 | public class MySQLiteOpenHelper extends SQLiteOpenHelper { |
12 | public static final String DATABASE_NAME = "AndyDemo.db" ; //数据库名 |
13 | public static final int VERSION = 1 ; //版本号 |
14 | public static final String TABLE_NAME = "andy" ; //表名 |
15 | public static final String ID = "id" ; |
16 | public static final String TEXT = "text" ; |
18 | public MySQLiteOpenHelper(Context context) { |
19 | super (context, DATABASE_NAME, null , VERSION); |
22 | * 在数据库第一次生成的时候会调用这个方法,同时我们在这个方法里边生成数据库表 |
25 | public void onCreate(SQLiteDatabase db) { |
27 | String strSQL = "CREATE TABLE " + TABLE_NAME + "(" + ID |
28 | + " INTEGER PRIMARY KEY AUTOINCREMENT," + TEXT + " text );" ; |
33 | * 更新或者升级数据库的时候会自动调用这个方法,一般我们会在这个方法中 |
37 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { |
38 | Log.e( "AndyDemo" , "onUpgrade" ); |
当我们需要把数据库创建在SDCard中时,需要在AndroidManifest.xml文件中添加对SDCard操作的权限,如下:1 | < uses-permission android:name = "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> |
2 | < uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" /> |
通过以上步骤的操作,对SQLite数据库的操作有了大概的认识。下面我们来看看程序运行的效果。
由于该Demo的代码比较多,需要该源码的可以发邮件给我索取。邮箱:Chenjunjun.ZJ@gmail.com
欢迎大家关注 ^-^
文章出处:http://blog.csdn.net/cjjky/article/details/6578393
|