一、進入資料庫
命令列輸入下列命令進入資料庫
1.adb
2.adb shell
3.cd data
4.cd data
5.cd [包名]
6.建立資料庫後
7.cd databases
8.sqlite3 [資料庫名]
即資料庫存放在/data/data/[包名]/databases/路徑下
常用命令
.schema 查看當前資料庫中有哪些表
查看錶中資料 select * from [tablename];
二、知識點
1、public void execSQL(String sql);
執行單個SQL語句,但不能執行SELECT和其他有傳回值的SQL語句;
2、SQL語句
語句:create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
說明:tabname——表名,col1,col2——欄位名(即列名),not null——該欄位非空,primary key——主鍵(某條記錄的唯一識別碼)。
3、public long insert (String table, String nullColumnHack, ContentValues values)
註:插入記錄,table——表名,nullColumnHack——強行插入null值的欄位(列名),values——一行記錄的資料。
4、public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
註:更新資料庫中資料;table——想要更新的表名,values——想要更新的資料,whereClause——滿足該whereClause子句的記錄將會被更新,whereArgs——為whereClause傳入參數。(可理解為:在表table中,根據whereClause和whereArgs尋找出資料,更新為values)
如:更新person_inf表中所有主鍵大於20的人的人名,
ContentValues values = new ContentValues();
values.put(“name”,“新人名”);
int result = db.update(“person_inf”,values,”_id > ?”, new Integer[]{20});
5、public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
註:查詢,詳情見協助。
6.SQLiteOpenHelper類中的onCreate方法
public abstract void onCreate (SQLiteDatabase db);
當調用SQliteOpenHelper的getWritableDatabase()或者getReadableDatabase()方法擷取用於操作資料庫的SQLiteDatabase執行個體時,如果資料庫不存在,Android系統會自動產生一個資料庫,接著調用onCreta方法,onCreate方法在初次產生資料庫時才被調用。
即:getWritableDatabase()、getReadableDatabase()在擷取執行個體時,資料庫不存在則建立,存在則不建立;而onCreate方法是第一次建立時自動調用的方法。因此,可重寫該方法初始化資料表結構及資料等。
三、資料庫版本version必須是正數!
四、掌握資料庫操作的兩個類SQLiteDatabase和SQLiteOpenHelper即可!
五、再理解資料庫和表的建立
六、執行個體代碼
package com.lucus.erseventh;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper{
private final static String DB_NAME = "erSeventh.db";//資料庫名稱
private final static String TABLE_NAME = "erSeventhTable";//表名稱
public final static String FIELD_ID="_id";//表列名,主鍵
public final static String FIELD_EVENT_NAME="eventName";//表列名
public final static String FIELD_REMIND_TIME="remindTime";//表列名
/**在SQLiteOpenHelper的子類中,必須有該建構函式;context即activity對象,name即資料庫名*/
public DatabaseHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, DB_NAME, factory, version);//調用父類的建構函式
// TODO Auto-generated constructor stub
}
/**在SQLiteOpenHelper的子類中,必須有該建構函式;context即activity對象,name即資料庫名*/
/**該函數是在第一次建立資料庫時執行,調用getWritableDatabase()或getReadableDatabase()方法時自動調用*/
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sql = " create table " +
TABLE_NAME +
" ( " +
FIELD_ID +
" integer primary key, " + //注意"前後有空格!!!
FIELD_EVENT_NAME +
" varchar, " +
FIELD_REMIND_TIME +
" varchar ) ";
db.execSQL(sql);
// db.execSQL("create table evtesttable(_id int primary key,eventName varchar(20)),remindTime varchar(20)");
// db.execSQL("create table eventName(_id int primary key,eventName varchar(20))");
System.out.println("Create a table");
}
/**該函數是在第一次建立資料庫時執行,調用getWritableDatabase()或getReadableDatabase()方法時自動調用*/
/**插入*/
public void insert(String myEventName, String myRemindTime)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(FIELD_EVENT_NAME, myEventName);
cv.put(FIELD_REMIND_TIME, myRemindTime);
db.insert(TABLE_NAME, null, cv);//第二個參數為null,主鍵自增1
db.close();
System.out.println("Insert data");
}
/**插入*/
/**修改,修改FIELD_ID為id的記錄,修改該記錄的FIELD_EVENT_NAME和FIELD_REMIND_TIM*/
public void update(int id,String newEventName,String newRemindTime)
{
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_ID + "=?";
String[] whereValue = {Integer.toString(id)};
ContentValues cv=new ContentValues();
cv.put(FIELD_EVENT_NAME, newEventName);
cv.put(FIELD_REMIND_TIME, newRemindTime);
db.update(TABLE_NAME, cv, where, whereValue);
System.out.println("Modify data");
}
/**修改,修改FIELD_ID為id的記錄,修改該記錄的FIELD_EVENT_NAME和FIELD_REMIND_TIM*/
/**刪除,刪除FIELD_ID為id的記錄*/
public void delete(int id)
{
SQLiteDatabase db=this.getWritableDatabase();
String where=FIELD_ID + "=?";
String[] whereValue={Integer.toString(id)};
db.delete(TABLE_NAME, where, whereValue);
System.out.println("Delete data");
}
/**刪除,刪除FIELD_ID為id的記錄*/
/**刪除資料庫?*/
public boolean deleteDBByName(String DBName) {
Context context = null;
context.deleteDatabase(DBName);
return false;
}
/**刪除資料庫?*/
/**擷取遊標*/
public Cursor getCursor()
{
SQLiteDatabase db=this.getWritableDatabase();
Cursor cursor=db.query(TABLE_NAME, null, null, null, null, null, null);
return cursor;
}
/**擷取遊標*/
/**查詢,查詢FIELD_ID為id的記錄*/
public Cursor query(int id)
{
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_ID + "=?";
String[] whereValue = {Integer.toString(id)};
Cursor cursor = db.query
(
TABLE_NAME,
new String []{"_id","eventName","remindTime"},
where,
whereValue,
null, null, null
);
return cursor;
}
/**查詢,查詢FIELD_ID為id的記錄*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}//end class DatabaseHelper