SQLite??Android???е???÷???
???????????? ???????[ 2013/4/28 10:00:53 ] ????????
???????????????????飬???????????????????????????????????onCreate?????????????????????????????????????????????汾??????????д?????????汾???£?????????????????????????(updated)??????????????£??????onUpgrade??????
????????????????????????????onCreate??????е??????????????????????????????????????????????????????????????????????????????????????Σ????????????????е????κ???????????????????Э??????????
????????????????????????????????????????????????£???????????????????????????????????????????SQL????????????
CREATE TABLE employees (
_id INTEGER PRIMARY KEY AUTOINCREMENT??
name TEXT NOT NULL??
ext TEXT NOT NULL??
mob TEXT NOT NULL??
age INTEGER NOT NULL DEFAULT '0'
);
???????????????????hard coding?????????????д????????У???ж????У????????£?
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(
"CREATE TABLE employees ( _id INTEGER PRIMARY KEY "
+ "AUTOINCREMENT?? name TEXT NOT NULL?? ext TEXT NOT NULL?? "
+ "mob TEXT NOT NULL?? age INTEGER NOT NULL DEFAULT '0')");
}
???????????????????????????????С???????????????????????????????????????????????????????????????SQL?????????asset????С????????????????????д?????????assets???ж??SQL???????????????
@Override
public void onCreate(SQLiteDatabase database) {
executeSQLScript(database?? "create.sql");
}
private void executeSQLScript(SQLiteDatabase database?? string dbname){
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
AssetManager assetManager = context.getAssets();
InputStream inputStream = null;
try{
inputStream = assetManager.open(dbname);
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf?? 0?? len);
}
outputStream.close();
inputStream.close();
String[] createScript = outputStream.toString().split(";");
for (int i = 0; i < createScript.length; i++) {
String sqlStatement = createScript[i].trim();
// TODO You may want to parse out comments here
if (sqlStatement.length() > 0) {
database.execSQL(sqlStatement + ";");
}
}
} catch (IOException e){
// TODO Handle Script Failed to Load
} catch (SQLException e) {
// TODO Handle Script Failed to Execute
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11