我正在尝试动态创建表并在需要时访问它们...但是当我运行代码时它既不会出错也不会产生错误,也不会创建数据库...通过调试我看到oncreate方法被调用了所有的遇到getWritableDatabase()
的时间。
这是我的SQLOpenHelper类 -
package com.example.launch;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class db extends SQLiteOpenHelper {
SQLiteDatabase db12;
public db(Context context) {
super(context, null, null, 1);
}
public void onCreate(SQLiteDatabase b) {
b.execSQL("CREATE TABLE IF NOT EXISTS sample1 (col1 TEXT_TYPE,col2 TEXT_TYPE,message TEXT_TYPE )");
}
public void create(String name) {
db12 = getWritableDatabase();
db12.execSQL("CREATE TABLE IF NOT EXISTS " + name
+ "(col1 TEXT_TYPE,col2 TEXT_TYPE,message TEXT_TYPE )");
db12.close();
Log.e("create", "successful");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insert(String name, String message, String col1, String col2) {
create(name);
db12 = getWritableDatabase();
ContentValues v = new ContentValues();
v.put("col1", col1);
v.put("col2", col2);
v.put("message", message);
db12.insert(name, null, v);
Log.e("insert", "may be successful");
db12.close();
}
public ArrayList<String> getArray() {
ArrayList<String> tableList = new ArrayList<String>();
String SQL_GET_ALL_TABLES = "SELECT name FROM "
+ "sqlite_master WHERE type='table' ORDER BY name";
db12 = getWritableDatabase();
Log.e("here", "came");
Cursor cursor = db12.rawQuery(SQL_GET_ALL_TABLES, null);
try {
if (cursor.moveToFirst())
;
{
while (!cursor.isAfterLast()) {
tableList.add(cursor.getString(0));
cursor.moveToNext();
}
}
} finally {
cursor.close();
db12.close();
}
return tableList;
}
public String gettable(String name) {
Cursor cursor = null;
String ss = "";
db12 = getWritableDatabase();
try {
cursor = db12.rawQuery("select * from " + name, null);
if (cursor.moveToFirst()) {
int col2in = cursor.getColumnIndex("col2");
int msg = cursor.getColumnIndex("message");
while (!cursor.isAfterLast()) {
if (cursor.getString(col2in) == "1") {
ss = ss + "/n " + name + " : " + cursor.getString(msg);
}
else {
ss = ss + "/n I : " + cursor.getString(msg);
}
}
}
} finally {
cursor.close();
db12.close();
}
return ss;
}
}
任何帮助表示赞赏....