我正在尝试在我的 android 应用程序中创建一个 SQLite 数据库,但我的代码始终抛出一个“SQLiteException”,表示不存在这样的表。我是 SQL 新手,但我认为这意味着我的创建脚本运行不正常。我的部分代码如下。请指出您看到的任何错误并提前感谢!
适配器:
public class DBAdapter
{
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_FAVORITE = "favorite";
public static final String KEY_LAT = "lat";
public static final String KEY_LONG = "long";
public static final String KEY_ADDRESS = "address";
public static final String KEY_PHONE = "phone";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "eateries";
private static final String DATABASE_TABLE = "names";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE + "( "
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_NAME + " text not null, "
+ KEY_FAVORITE + " integer, "
+ KEY_LAT + " text not null, "
+ KEY_LONG + " text not null, "
+ KEY_ADDRESS + " text not null, "
+ KEY_PHONE + " text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS names");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a title into the database---
public long insertNew(String name, Integer favorite, GeoPoint coords, String address, String phone)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_FAVORITE, favorite);
initialValues.put(KEY_LAT, coords.getLatitudeE6());
initialValues.put(KEY_LONG, coords.getLongitudeE6());
initialValues.put(KEY_ADDRESS, address);
initialValues.put(KEY_PHONE, phone);
return db.insertOrThrow(DATABASE_TABLE, null, initialValues);
}
调用代码:包bhekman.test.database;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
public class DatabaseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBAdapter dba = new DBAdapter(this);
dba.open();
dba.insertNew("mc. awesome", 1, new GeoPoint(0, 0), "42 SUPER AWESOME DRIVE", "(616)-994-2421");
Toast.makeText(this, dba.getName(0).getString(0), Toast.LENGTH_LONG).show();
dba.close();
}
}
LogCat 错误:
01-05 22:06:29.030: E/AndroidRuntime(4785): FATAL EXCEPTION: main
01-05 22:06:29.030: E/AndroidRuntime(4785): java.lang.RuntimeException: Unable to start activity ComponentInfo{bhekman.test.database/bhekman.test.database.DatabaseActivity}: android.database.sqlite.SQLiteException: no such table: names: , while compiling: INSERT INTO names(phone, long, favorite, address, lat, name) VALUES(?, ?, ?, ?, ?, ?);
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.os.Looper.loop(Looper.java:130)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-05 22:06:29.030: E/AndroidRuntime(4785): at java.lang.reflect.Method.invokeNative(Native Method)
01-05 22:06:29.030: E/AndroidRuntime(4785): at java.lang.reflect.Method.invoke(Method.java:507)
01-05 22:06:29.030: E/AndroidRuntime(4785): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-05 22:06:29.030: E/AndroidRuntime(4785): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-05 22:06:29.030: E/AndroidRuntime(4785): at dalvik.system.NativeStart.main(Native Method)
01-05 22:06:29.030: E/AndroidRuntime(4785): Caused by: android.database.sqlite.SQLiteException: no such table: names: , while compiling: INSERT INTO names(phone, long, favorite, address, lat, name) VALUES(?, ?, ?, ?, ?, ?);
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1452)
01-05 22:06:29.030: E/AndroidRuntime(4785): at bhekman.test.database.DBAdapter.insertNew(DBAdapter.java:93)
01-05 22:06:29.030: E/AndroidRuntime(4785): at bhekman.test.database.DatabaseActivity.onCreate(DatabaseActivity.java:18)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-05 22:06:29.030: E/AndroidRuntime(4785): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-05 22:06:29.030: E/AndroidRuntime(4785): ... 11 more