我一直在使用这个教程,但我不得不做一些小改动,我的应用程序在手机上崩溃了。但是,代码中没有错误! 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);}