我继续收到此错误,说我的数据库中的表不存在,但表已声明。
所以,我有这个类在数据库中创建约会,即:
package com.example.calendar;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.EditText;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.calendar.Appointment;
import static com.example.calendar.Constants.KEY_DATE;
import static com.example.calendar.Constants.KEY_DETAILS;
import static com.example.calendar.Constants.KEY_ID;
import static com.example.calendar.Constants.KEY_TIME;
import static com.example.calendar.Constants.KEY_TITLE;
import static com.example.calendar.Constants.TABLE_APP;
public class CreateAppointment extends Activity implements OnClickListener{
EditText titleTextBox, timeTextBox, detailsTextBox;
int[] dateSelected = new int[3];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
titleTextBox = (EditText) findViewById(R.id.apptTitle);
timeTextBox = (EditText) findViewById(R.id.apptTime);
detailsTextBox = (EditText) findViewById(R.id.apptDetails);
View createButton = (Button) findViewById(R.id.apptSave);
String dateToPass = null;
Bundle getExtraFromMain = getIntent().getExtras();
if(getExtraFromMain != null){
dateSelected = getExtraFromMain.getIntArray(dateToPass);
}
createButton.setOnClickListener(this);
}
private Cursor cursor(){
DBHandler appointment = new DBHandler(this);
SQLiteDatabase db = appointment.getReadableDatabase();
String[] getValueFrom = {KEY_DATE, KEY_TITLE, KEY_TIME, KEY_DETAILS};
Cursor cursor = db.query(TABLE_APP, getValueFrom, null, null, null, null, KEY_DATE + " ASC");
return cursor;
}
private void save(){
String getTime = timeTextBox.getText().toString();
String getTitle = titleTextBox.getText().toString();
String getDetails = detailsTextBox.getText().toString();
DBHandler appointment = new DBHandler(this);
SQLiteDatabase db = appointment.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, getTitle);
values.put(KEY_TIME, getTime);
values.put(KEY_DETAILS, getDetails);
values.put(KEY_DATE, dateSelected[0] + "/" + dateSelected[1] + "/" + dateSelected[2]);
db.insertOrThrow(TABLE_APP, null, values);
appointment.close();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.apptSave:
save();
finish();
break;
}
}
}
当然,我的经纪人:
package com.example.calendar;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import static com.example.calendar.Constants.KEY_DATE;
import static com.example.calendar.Constants.KEY_DETAILS;
import static com.example.calendar.Constants.KEY_ID;
import static com.example.calendar.Constants.KEY_TIME;
import static com.example.calendar.Constants.KEY_TITLE;
import static com.example.calendar.Constants.TABLE_APP;
public class DBHandler extends SQLiteOpenHelper {
//Variables needed
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "appts.db";// database name
public DBHandler(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override//create table
public void onCreate(SQLiteDatabase db){
String CREATE_APPOINTMENTS_TABLE = "CREATE TABLE " + TABLE_APP + " ("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DATE + " TEXT NOT NULL,"
+ KEY_TITLE + " TEXT NOT NULL, " + KEY_TIME + " TEXT NOT NULL, " + KEY_DETAILS + " TEXT NOT NULL" + ");";
db.execSQL(CREATE_APPOINTMENTS_TABLE);
}
@Override//when upgraded, trigger
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS " + TABLE_APP);//delete table from older version
onCreate(db);//create new table
}
}
此外,声明表名的接口:
package com.example.calendar;
import android.provider.BaseColumns;
public interface Constants extends BaseColumns{
public static final String TABLE_APP = "appointments";// table name
public static final String KEY_ID = "id";
public static final String KEY_DATE = "date";
public static final String KEY_TITLE = "title";
public static final String KEY_TIME = "time";
public static final String KEY_DETAILS = "details";
}
当我点击Activity
上的按钮时,Logcat给了我这个:
03-24 02:18:28.134: E/AndroidRuntime(800): android.database.sqlite.SQLiteException: no such table: appointments: , while compiling: INSERT INTO appointments(time,title,date,details) VALUES (?,?,?,?)
03-24 02:18:28.134: E/AndroidRuntime(800): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-24 02:18:28.134: E/AndroidRuntime(800): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
03-24 02:18:28.134: E/AndroidRuntime(800): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:112)
03-24 02:18:28.134: E/AndroidRuntime(800): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
03-24 02:18:28.134: E/AndroidRuntime(800): at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1617)
03-24 02:18:28.134: E/AndroidRuntime(800): at com.example.calendar.CreateAppointment.save(CreateAppointment.java:69)
03-24 02:18:28.134: E/AndroidRuntime(800): at com.example.calendar.CreateAppointment.onClick(CreateAppointment.java:80)
注意:第69和80行是:
69:
db.insertOrThrow(TABLE_APP, null, values);
80:
save();//from switch statement
有人可以帮助我吗?
答案 0 :(得分:3)
您的代码看起来很好,因此将DATABASE_VERSION = 1
更改为DATABASE_VERSION = 2
可能会有所帮助。
原因是onCreate()
和onUpgrade()
只运行一次!因此,如果您在调用它们之后对其进行了任何更改,则更改将不会生效。因此,更改数据库的版本或清除设备/模拟器中的应用程序数据可能会触发再次运行onCreate()
和onUpgrade()
。
因此,每当您更改onCreate()
和onUpgrade()
内的任何内容时,都会增加数据库版本。