我正在尝试在我的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);
}
致电代码: package 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
答案 0 :(得分:4)
使用您的代码在我的计算机上运行正常。 所以我想你在开发过程中更改了TABLE名称。然后使用eclipse的RUN命令再次运行程序。所以应用程序不是全新安装。但是你没有正确实现onUpgrade()。
解决您遇到的问题。
答案 1 :(得分:0)
您最初创建表后是否对表进行了更改(意味着在手机/模拟器上运行)?比如可能添加一个新表,或更改它的名称?
如果是这样,您需要将DATABASE_VERSION
更改为2,以便将数据库升级到最新架构。
KEY_FAVORITE + " bit, "
需要KEY_FAVORITE + " integer, "
。如果您进行了更改,则还需要更新DATABASE_VERSION。
基本上对于改变数据库结构的任何,您需要增加版本,以便数据库使用您的新更改重新创建数据库。