利用genymotion內的tool adb.exe 可以去新增sqlite的資料庫
(cmd -> adb.exe shell -> sqlite3)
在Android開發端也可以利用以下指令建立資料庫
class my_open_helper extends SQLiteOpenHelper{
String database_name;
int ver;
public my_open_helper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
database_name = name;
ver = version;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table student(_id integer primary key autoincrement, name string, score int, picture blob);");
//新增一些基礎資料(隨便你要不要)
byte[] john_byte;
Resources res = MainActivity.this.getResources();
Bitmap john = BitmapFactory.decodeResource(res,R.drawable.john);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
john.compress(Bitmap.CompressFormat.JPEG, 0, outputStream);
john_byte=outputStream.toByteArray();
ContentValues data =new ContentValues();
data.put("name","john");
data.put("score", 99);
data.put("picture", john_byte);
db.insert("student",null,data);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
上面的範例是建立一個自己的openhelper
利用下面的程式碼他就會去檢查
若沒有資料庫存在則會在上面的OnCreate 新增
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
my_open_helper helper= new my_open_helper(this,"test",null,1);
SQLiteDatabase db;
db = helper.getReadableDatabase();
}
留言列表