Android儲存方式之SQLite的使用

來源:互聯網
上載者:User

Android提供了五種儲存方式,分別是檔案、sharedPreference、網路、SQLite、ContentProvider。SQLite是一種輕型資料庫,具有獨立性、隔離性、跨平台、多語言介面、安全性等優點,目前應用較為廣泛。現在主要說一下在Android中SQLite的使用。 首先,我建立一個資料庫操作類DataBaseHelper,讓他繼承自SQLiteOpenHelper,然後複寫其中的兩個方法onCreate和onUpgrade。 複製代碼 1 package com.example.sqlitedb; 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 DataBaseHelper extends SQLiteOpenHelper{ 9     10     private final static String DBName="sqlite3.db";11     private final static String TableName="";12     private final static String firstname="provincial";13     private final static String lastname="number";14 15     public DataBaseHelper(Context context, String name, CursorFactory factory,16             int version) {17         super(context, DBName, factory, version);18         // TODO Auto-generated constructor stub19     }20 21     @Override22     public void onCreate(SQLiteDatabase db) {23         //建立表24         String sql=("CREATE TABLE"+TableName+"(INTEGER PRIMARY KEY AUTOINCREMENT,"+firstname+" VARCHAR, "+lastname+" SMALLINT)");25         db.execSQL(sql);26     }27 28     @Override29     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {30         // 刪除表31         String sql = "DROP TABLE IF EXISTS " + TableName; 32         db.execSQL(sql);34     }35 36 }複製代碼然後,使用sql語句來實現一些常用的SQLite的操作,具體如下: 複製代碼 1     @Override 2     public void onCreate(SQLiteDatabase db) { 3           //建立表 4           String sql="CREATE TABLE city (INTEGER PRIMARY KEY AUTOINCREMENT, provincial VARCHAR, number SMALLINT)"; 5           db.execSQL(sql); 6           //插入資料 7           //方法1:使用execSQL 8           String sql1="insert into city (firstname,lastname) values ('北京','20')"; 9           db.execSQL(sql1);10           //方法2:使用insert11           ContentValues cv=new ContentValues();12           cv.put("provincial", "天津");13           cv.put("number", 30);14           db.insert("city", null, cv);15           //修改資料16           //1.使用execSQL17           String sql2="update city set lastname = '25' where provincial='天津'";18           db.execSQL(sql2);19           //2.使用insert20           ContentValues cv1=new ContentValues();21           cv1.put("provincial", "北京");22           cv1.put("number", 30);23           db.insert("city", null, cv);24           //刪除資料25           String sql3 = "delete from city where provincial='北京'";26           db.execSQL(sql3);27           //關閉資料庫28           db.close();29     }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.