我正在尝试做的是创建一个表,然后在其中插入一些值然后选择。 我面临的问题是应用程序不幸停止。我不知道是什么问题。
我的代码是: 该类调用数据库处理程序
public class SaveEvents extends Activity {
private static final String DB_NAME = "EventDB";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_product);
Bundle extras=getIntent().getExtras();
String event=extras.getString("event");
String date=extras.getString("date");
String college=extras.getString("college");
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
db.insert_data(event,date,college);
String c=db.select_data();
Toast.makeText(getApplicationContext(), "EventName: "+c+"", Toast.LENGTH_LONG).show();
}
}
数据库处理程序
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "EventDB";
private static final String TABLE_NAME = "eventlist";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Category table create query
String CREATE_ITEM_TABLE;
CREATE_ITEM_TABLE = "CREATE TABLE eventlist(eventname varchar(100),eventdate varchar(100),college varchar(100));";
db.execSQL(CREATE_ITEM_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
void insert_data(String eventname,String date,String college)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("eventname", eventname);
values.put("eventdate",date);
values.put("college",college);
db.insert(TABLE_NAME,null,values);
db.close();
}
public String select_data()
{
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT eventname FROM eventlist where eventname='techcontest'";
String c=null;
Cursor cursor = db.rawQuery(selectQuery, null);
cursor.moveToFirst();
c=cursor.getString(0);
return c;
}
}
我的logcat显示
03-16 20:00:19.116 8378-8378/com.example.android E/SQLiteLog﹕ (1) no such table: eventlist
03-16 20:00:19.122 8378-8378/com.example.android E/SQLiteDatabase﹕ Error inserting college=acs university eventdate=2-02-15 eventname=techocontest
android.database.sqlite.SQLiteException: no such table: eventlist (code 1): , while compiling: INSERT INTO eventlist(college,eventdate,eventname) VALUES (?,?,?)
答案 0 :(得分:0)
看起来你在select_data()方法中关闭数据库。