天天看點

android系統中建立資料庫,并建立表

android系統

android開發中sqlite3支援的資料類型:

NULL、INTEGER、REAL、TEXT、BLOB

但是,sqlite3也支援如下的資料類型

smallint           16位整數

integer             32位整數

decimal(p,s)   p是精确值,s是小數位數

float 32位實數

double             64位實數

char(n)             n長度字元串,不能超過254

varchar(n)        長度不固定最大字元串長度為n,n不超過4000

graphic(n)        和 char(n) 一樣,但是機關是兩個字元double-bytes,n不超過127(中文字)

vargraphic(n)  可變長度且最大長度為n

建立一個資料庫

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteDatabase.CursorFactory;

import android.database.sqlite.SQLiteOpenHelper;

public abstract class shujukujiekou extends SQLiteOpenHelper {

  public shujukujiekou(Context context, String name, CursorFactory factory,

   int version) {

  super(context, name, factory, version);

  // TODO Auto-generated constructor stub

 }

 private static final String DATABASENAME = "test.db"; //資料庫名稱

 private static final int DATABASEVERSION = 1;//資料庫版本,大于0

 public shujukujiekou(Context context) {

 super(context, DATABASENAME, null, DATABASEVERSION);

 }

 public void onCreate(SQLiteDatabase db) {

  db.execSQL("CREATE TABLE person (personid integer primary key autoincrement, name varchar(20), amount integer)");//建立表 id ,name, imageid

     db.execSQL("CREATE TABLE liaotianjilu(personid integer primary key autoincrement, date varchar(50), words varchar(20),dir int)");

     //建立表 id,date, words,  dir

   }

 }

注:sqlite3資料庫中并不支援bool型資料

首次調用類,則初始化操作,建立資料庫和資料表