这是我想要创建数据库表的MainActivity的提取
public class GameDrive extends Activity {
DatabaseHelper databaseHelper;
SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_drive);
getActionBar().setDisplayHomeAsUpEnabled(true);
databaseHelper = new DatabaseHelper(this);
database = databaseHelper.getWritableDatabase();
。 。
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABSE_NAME = "database";
private static final String TABLE_NAME = "tripdata";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TYP = "typ";
private static final String COLUMN_VALUE = "value";
private static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context){
super(context, DATABSE_NAME, null, DATABASE_VERSION);
Log.d("Database","Constructor called");
}
//database is created for first time. creation of table and initial data inside done here
@Override
public void onCreate(SQLiteDatabase db) {
Log.d("Database","onCreate called");
db.execSQL("CREATE TABLE "+TABLE_NAME+"("+COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+COLUMN_TYP+" VARCHAR(255),"+COLUMN_VALUE+" VARCHAR(255));" );
Log.d("Database","onCreate called succesfuly");
}
//drop tables, add tables etc.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
try {
db.execSQL("DROP TABLE IF EXISTS TABLE_NAME");
onCreate(db);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
为什么从未到达Log.d(“数据库”,“onCreate called”)?启动应用程序后,只需达到Log.d(“数据库”,“构造函数调用”)语句。
谢谢!
答案 0 :(得分:0)
我猜这张桌子已经存在了。所以onDreate不会再被执行。尝试安装SQLite Viewer以更好地可视化您的数据库。 我也无法在DatabaseHelper中看到方法getWritableDatabase()。请检查一下。
您可以尝试的另一件事是将create table命令修改为create table(如果不存在)。