Android SQLite資料庫操作

來源:互聯網
上載者:User

標籤:android   style   blog   class   code   java   

更多內容歡迎訪問我的個人網站:www.qingshuimonk.com查看

 

SQLite是一種輕量級關係型資料庫,目前支援NULL,INTEGER,REAL(浮點),TEXT(字串文本),BLOB(二進位文本)這五種類型。

建立的資料庫檔案位於/data/data/package-name/databases目錄下。

在Eclipse中可以通過File Explorer訪問檔案。

 

對SQLite資料庫操作的步驟:

1.建立SQLiteOpenHelper類,用於初始化、升級資料庫。

 1 package com.qingshuimonk.radarmonitor; 2  3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteDatabase.CursorFactory; 6 import android.database.sqlite.SQLiteOpenHelper; 7  8 public class MySQLiteHelper extends SQLiteOpenHelper{ 9     10     public MySQLiteHelper(Context context, String name, CursorFactory factory, int version){11         super(context, name, factory, version);12     }13 14     @Override15     public void onCreate(SQLiteDatabase db) {16         // TODO Auto-generated method stub17         db.execSQL("create table datatable(name text, value real)");18     }19 20     @Override21     // 資料庫版本不一致時,磁碟上的資料庫版本需要升級到目前的版本時調用22     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {23         // TODO Auto-generated method stub24         // 刪除舊錶,建立新表25         //db.execSQL("DROP TABLE IF IT EXISTS " + DATABASE_TABLE);26         //onCreate(db);27     }28     29     30 31 }

其中

  db.execSQL("create table datatable(name text, value real)");

指令用於建立資料庫及表格,資料庫名稱為datatable,表格中內容有一個字串文本name,一個浮點數字value。一般在使用中還會定義一個

_id integer primary key autoincrement欄位

用於表示表格的行號。

 

2.建立或開啟資料庫

建立變數:

1 public MySQLiteHelper mySQLiteHelper;2 public SQLiteDatabase datadb;

建立資料庫:

1 // 建立資料庫2 mySQLiteHelper = new MySQLiteHelper(RegionActivity.this, "datadb", null, 1);3 datadb = mySQLiteHelper.getReadableDatabase();

MySQLiteHelper的形參分別為:context,資料庫名稱,工廠(預設為null),版本

 

3.資料庫的基本操作:

建立:datadb.execSQL("create table mytable(_id integer primary key autoincrement, name text, value real)");

關閉:datadb.close;

增加:

  ContentValues cv = new ContentValues();

  cv.put("_id",1);

  cv.put("name","length");

  cv.put("value",2);

  datadb.inset("mytable",null,cv);

查詢:

  需要建立一個指向資料表的指標cursor,然後通過指標取得列的索引index,將index傳入相應的方法,取得想要的資料。

  Cursor cursor = datadb.query("mytable", new String[]{"_id","name","value"},null,null,null,null,null);

  while(cursor.moveToNext()){

    int idindex = cursor.getColumnIndex("_id");

    int id = cursor.getInt(idindex);

 

    int nameindex = cursor.getColumnIndex("name");

    String name = cursor.getString(nameindex);

 

    int valueindex = cursor.getColumnIndex("value");

    float value = cursor.getFloat(valueindex);

  }

  cursor的一些相應方法還有:

  moveToNext();

  moveToPrevious();

  moveToFirst();

  moveToLast();

  moveToPosition();

  getCount();

  getPosition();

  getColumnIndexOrThrow();

  getColumnName();

  getColumnNames();

  各方法使用詳情參見Android Developers。

修改:

  String updateString = "update mytable set name=‘length‘, value=‘12‘ where _id=1";

  datadb.execSQL(updateString);

刪除:

  String deleteString="delete from mytable where _id=1";

  datadb.execSQL(deleteString);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.