1

我正在尝试在我的 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
4

2 回答 2

4

使用您的代码,它在我的计算机上运行良好。所以我猜你在开发过程中更改了 TABLE 名称。只需使用 Eclipse 的 RUN 命令再次运行该程序。因此,该应用程序不是全新安装。但是您没有正确实现 onUpgrade() 。

快速解决您遇到的问题。

  1. 使用 adb uninstall 卸载程序
  2. 重新安装程序。
于 2012-05-13T02:30:37.130 回答
0

您是否在最初创建表格后对其进行了更改(意味着在您的手机/模拟器上运行它)?比如可能添加一个新表,或者更改它的名称?

如果是这样,您需要将您的更改DATABASE_VERSION为 2,以便将数据库升级到您的最新架构。

并且 KEY_FAVORITE + " bit, "需要 KEY_FAVORITE + " integer, "。如果你做出改变,你也需要更新你的 DATABASE_VERSION。

基本上,对于任何对数据库结构进行更改的内容,您都需要增加版本,以便数据库将使用您的新更改重新创建数据库。

于 2012-05-13T02:52:11.563 回答