標籤:
一:基本操作1繼承SQLiteOpenHelper
public class UserSqliteOpenHelper extends SQLiteOpenHelper { private static final int versionNo = 1; public UserSqliteOpenHelper(Context context) { super(context, "user.db", null, versionNo); } @Override public void onCreate(SQLiteDatabase db) { Log. i("" , "sqlite onCreate" ); db.execSQL( "create table user (id Integer primary key autoincrement, name varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log. i("" , "sqlite onUpgrade" ); db.execSQL( "insert into user(name) values(?)", new Object[]{"version"+versionNo }); } }
從代碼中我們可以看出,UserSqliteOpenHelper重寫了SQLiteOpenHelper的onCreate和onUpgrade兩個方法說明:onCreate:資料庫被建立的時候調用,一般執行建立資料庫語句onUpgrade:當版本號碼(
versionNo)增加時執行此方法,一般用於對錶結構的更改、添加或刪除*第一次建立資料庫時執行onCreate,不執行onUpgrade;更改版本號碼之後,執行onUpgrade,不執行onCreate操作說明:
當 private
static
final
int
versionNo = 1 時結果如下
當 private
static
final
int
versionNo = 2 時結果如下 2.建立dao檔案對sqlite資料庫進行操作
public class UserDao { private UserSqliteOpenHelper tsoh; public UserDao(Context context) { tsoh = new UserSqliteOpenHelper(context); } public void insert(String name){ SQLiteDatabase db = tsoh.getWritableDatabase(); db. execSQL("insert into user(name) values(?)", new Object[]{name}); db.close(); } public boolean find(String name){ SQLiteDatabase db = tsoh.getReadableDatabase(); Cursor cursor = db.rawQuery( "select * from user where name = ?", new String[]{name}); boolean result = cursor.moveToNext(); cursor.close(); db.close(); return result; } public void update(String name, String newName){ SQLiteDatabase db = tsoh.getWritableDatabase(); db. execSQL("update user set name = ? where name = ?", new Object[]{newName, name}); db.close(); } public void delete(String name){ SQLiteDatabase db = tsoh.getWritableDatabase(); db. execSQL("delete from user where name = ?", new Object[]{name}); db.close(); } public List<User> findAll(){ List<User> users = new ArrayList<User>(); SQLiteDatabase db = tsoh.getReadableDatabase(); Cursor cursor = db.rawQuery( "select * from user", null); while(cursor.moveToNext()){ int id = cursor.getInt(cursor.getColumnIndex("id" )); String name = cursor.getString(cursor.getColumnIndex("name" )); User user = new User(id, name); users.add(user); } cursor.close(); db.close(); return users; } }
user類的定義
public class User { private int id; private String name; public User() { super(); } public User(int id, String name) { super(); this. id = id; this. name = name; } public int getId() { return id; } public void setId(int id) { this. id = id; } public String getName() { return name; } public void setName(String name) { this. name = name; } @Override public String toString() { return "info:"+ id+ ","+ name; }}
代碼很簡單,不做過多解釋。需要注意的是,每次在操作資料庫的時候記得將cursor和database關閉,釋放資源。項目級的代碼應該使用try catch finally來操作,這裡為了示範簡單操作。這裡還是沿用了拼接字串的方式執行sql語句,其實android還為我們提供封裝好了的API(最終還是將參數拼接成字串,不過對於調用者來說新的API簡單明了,使用者不用考慮單引號雙引號的麻煩了),這裡不做過多介紹。 3.調用代碼
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. activity_main); UserDao testDao = new UserDao( this); testDao.insert( "name1"); testDao.insert( "name2"); testDao.insert( "name3"); testDao.update( "name1", "name4"); boolean result = testDao.find("name2" ); testDao.delete( "name3"); List<User> users = testDao.findAll(); for(User user:users){ Log. i("MainActivity" , user.toString()); } }
基本操作介紹完畢。 二:推薦sqlite介面工具:sqlite expert professional前兩天有個同事問我,sqlite中Integer能儲存最大的數值是多少,當時我也不知道,於是就當場測試了一下,測試的結果:-9223372036854775808 ~ 9223372036854775807 也就是-2^63到2^63-1這就是我通過 sqlite工具實驗的結果
INTEGER: 用來儲存一個整數,根據大小可以使用1,2,3,4,6,8個位元組來儲存(網路抄錄,從我實驗的結果上來看上限是8個位元組的整數)
sqlite的基本使用