我一直在使用本教程,但我不得不做一些小改动,我的应用程序在手机上崩溃了。但是代码中没有错误! http://www.vogella.com/articles/AndroidSQLite/article.html
public class SQLiteDbHelper extends SQLiteOpenHelper {
public static final String TABLE_NAME = "table";
public static final String COLUMN_NAME_SP = "SP";
public static final String COLUMN_NAME_ST = "ST";
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "SPT.db";
private static final String DATABASE_CREATE =
"CREATE TABLE " + TABLE_NAME +
" (" +
COLUMN_NAME_SP + " text not null" +
COLUMN_NAME_ST + " text not null" +
" )";
public SQLiteDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
Log.w(SQLiteDbHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}
}
然后,稍后,我有一个按钮点击方法的代码:
public void Save(View view){
String st = editTextSt.getText().toString();
String sp = editTextSp.getText().toString();
//this is what makes my app crash [beginning]
SQLiteDbHelper database2 = new SQLiteDbHelper(this);
SQLiteDatabase database = database2.getWritableDatabase();
//this is what makes my app crash [end]
ContentValues values = new ContentValues();
values.put(SQLiteDbHelper.COLUMN_NAME_SP, sp);
values.put(SQLiteDbHelper.COLUMN_NAME_ST, st);
long insertId = database.insert(SQLiteDbHelper.TABLE_NAME, null, values);}
答案 0 :(得分:0)
您忘记在create table query中的每个列名后面放置逗号(,)
,将其更改为:
private static final String DATABASE_CREATE =
"CREATE TABLE " + TABLE_NAME +
" (" +
COLUMN_NAME_SP + " text not null, " +
COLUMN_NAME_ST + " text not null " +
");";