Android 学习笔记之 SQLite基础用法

SQLite是Android中的轻量级的数据库,其基本操作有增、删、查、改。每一种操作都有两个方法,一种是通过SQL语句来执行,一种是用Android提供的方法。

一、创建数据库(数据库只创建一次)

 1 public class DBHelper extends SQLiteOpenHelper {

 2 

 3     private static final String DB_NAME = "Test.db";// 数据库名称

 4     private static final String TBL_NAME_TEST = "TestTabName"; // 表名称

 5     // 创建数据库的SQL语句

 6     private static final String CREATE_TBL_TEST = "create table TestTabName(_id integer primary key autoincrement,TestNum text,TestName text)";

 7     private SQLiteDatabase db;

 8 

 9     /**

10      * 构造函数

11      * 

12      * @param context

13      *            上下文

14      * @param name

15      *            数据库名称

16      * @param factory

17      * @param version

18      *            版本号

19      */

20     public DBHelper(Context context) {

21         super(context, DB_NAME, null, 2);

22         // TODO Auto-generated constructor stub

23     }

24 

25     // 创建数据库

26     @Override

27     public void onCreate(SQLiteDatabase db) {

28         // TODO Auto-generated method stub

29         this.db = db;

30         // 创建表

31         db.execSQL(TBL_NAME_TEST);

32 

33     }

34 

35     // 数据库更新

36     @Override

37     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

38         // TODO Auto-generated method stub

39 

40     }

二、数据库的操作

1>增(也就是向指定的数据库中插入一条数据)

 1 /**

 2      * 向指定数据库中插入一条数据

 3      * 

 4      * @param values

 5      *            ContentValues 键值对, 相当于map

 6      * @param tableName

 7      *            表名称

 8      */

 9     public void insert(ContentValues values, String tableName) {

10 

11         SQLiteDatabase db = getWritableDatabase();

12         db.insert(tableName, null, values);

13         db.close();

14 

15     }

2>删(可以删除表中所有数据,也可以指定满足条件的数据)

 1 /**

 2      * 根据ID删除一条数据

 3      * 

 4      * @param id

 5      * @param tableName

 6      */

 7     public void del(int id, String tableName) {

 8 

 9         if (db == null)

10             db = getWritableDatabase();

11         db.delete(tableName, "_id=?", new String[] { String.valueOf(id) });

12 

13     }

14 

15     /**

16      * 删除表中所有数据

17      * 

18      * @param tableName

19      */

20     public void delAll(String tableName) {

21 

22         if (db == null)

23             db = getWritableDatabase();

24         String sql = "Delete from " + tableName;

25         try {

26             db.execSQL(sql);

27         } catch (Exception e) {

28             // TODO: handle exception

29             System.out.print(e);

30         }

31 

32     }

3>改(更新一条指定的数据)

 1 /**

 2      * 更新一条数据

 3      * 

 4      * @param values

 5      *            要更新的数据

 6      * @param id

 7      *            更新的条件

 8      * @param tableName

 9      *            更新的表名称

10      * @return

11      */

12     public boolean updataData(ContentValues values, int id, String tableName) {

13         boolean bool = false;

14         SQLiteDatabase db = getWritableDatabase();

15         bool = db.update(tableName, values, "_id=" + id, null) > 0;

16         return bool;

17 

18     }

4>查(查询数据,返回的是游标类型的数据,对它进行读取,打开一个游标,当结束后要关闭游标)

 1 /**

 2      * 返回表中所有数据

 3      * 

 4      * @param tableName

 5      *            表名称

 6      * @return

 7      */

 8     public List<String> quertAll(String tableName) {

 9         List<String> list = new ArrayList<String>();

10         Cursor c = null;

11         SQLiteDatabase db = getWritableDatabase();

12         c = db.query(tableName, null, null, null, null, null, null);

13         // 提取游标中的值

14         try {

15             for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {

16                 // 根据列名获取数据

17                 String TestNum = c.getString(c.getColumnIndex("TestNum"));

18                 list.add(TestNum);

19             }

20         } catch (Exception e) {

21             // TODO: handle exception

22         } finally {

23             //关闭游标

24             c.close();

25         }

26 

27         return list;

28     }

根据条件获取表中的值

 1 /**

 2      * 根据条件查询数据

 3      * 

 4      * @param where

 5      *            条件

 6      * @param tableName

 7      *            表名称

 8      * @return   这边我就不获取游标中的值了,同上。

 9      */

10     public Cursor quertToWhere(String where, String tableName) {

11 

12         Cursor c = null;

13         SQLiteDatabase db = getWritableDatabase();

14         try {

15             c = db.query(tableName, null, where, null, null, null, null);

16         } catch (Exception e) {

17             // TODO: handle exception

18             String msg = e.toString();

19             Log.i("", msg);

20         }

21         return c;

22     }

由于我也是个菜鸟,都是比较基础的知识,希望能帮助和我一样菜的同孩们。。。

 

你可能感兴趣的:(android)