1

执行我的 android 应用程序时出现此错误。我在一个名为 MySQLitehelper.java 的文件中创建了 SQLite 数据库,该文件具有以下用于创建表的行:

 public static final String COLUMN_ID = "GWid";
  public static final String COLUMN_DATE = "DateGWU";
  public static final String COLUMN_LOCATION = "location";
  public static final String COLUMN_TIME = "time";
  
  public static final String TABLE_NAME = "UPDTable";

  private static final String DATABASE_NAME = "UPDdb_version6";
  private static final int DATABASE_VERSION = 6;
  
  private final Context context;
  GetSet getset = new GetSet();
  public void GetIdForGwid(GetSet get)
  {
     getset=get; 
  }
 
  // Database creation sql statement
  private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_NAME +
                                " (" +COLUMN_ID+ " integer," + COLUMN_DATE + "VARCHAR," +
                                COLUMN_LOCATION+" VARCHAR," +COLUMN_TIME +" VARCHAR);";
  
  private static final String DATABASE_INSERT = "INSERT INTO " +TABLE_NAME +
                                                " Values (47688507,'DEC-07-2012','MARVIN 203','20:00');";
 

在同一个文件中,我有执行我的 SQL SELECT 查询的语句,但给我一个例外,没有找到这样的列 DateGWU

public Cursor getRecord() throws SQLException
{
        Cursor mCursor =
        db.query(true, TABLE_NAME, new String[] {COLUMN_ID,
        COLUMN_DATE, COLUMN_LOCATION, COLUMN_TIME},
        COLUMN_ID + "= 47688507", null, null, null, null, null);   //HARDCODED. Please make it dynamic
        if (mCursor != null) 
        {
            mCursor.moveToFirst();
        }
 return mCursor;
 }

我有点卡住了,不知道我哪里错了。

LOGCAT

11-30 04:57:50.832: E/SQLiteLog(12912): (1) no such column: DateGWU
11-30 04:57:50.842: D/AndroidRuntime(12912): Shutting down VM
11-30 04:57:50.852: W/dalvikvm(12912): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
11-30 04:57:50.952: E/AndroidRuntime(12912): FATAL EXCEPTION: main
11-30 04:57:50.952: E/AndroidRuntime(12912): android.database.sqlite.SQLiteException: no such column: DateGWU (code 1): , while compiling: SELECT DISTINCT GWid, DateGWU, location, time FROM UPDTable WHERE GWid= '47688507'
11-30 04:57:50.952: E/AndroidRuntime(12912):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-30 04:57:50.952: E/AndroidRuntime(12912):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
11-30 04:57:50.952: E/AndroidRuntime(12912):    at  android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
11-30 04:57:50.952: E/AndroidRuntime(12912):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-30 04:57:50.952: E/AndroidRuntime(12912):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-30 04:57:50.952: E/AndroidRuntime(12912):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
11-30 04:57:50.952: E/AndroidRuntime(12912):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
11-30 04:57:50.952: E/AndroidRuntime(12912):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
11-30 04:57:50.952: E/AndroidRuntime(12912):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
11-30 04:57:50.952: E/AndroidRuntime(12912):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
11-30 04:57:50.952: E/AndroidRuntime(12912):    at com.example.upd.MySQLitehelper.getRecord(MySQLitehelper.java:125)
4

3 回答 3

7

你有

private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_NAME +
                            " (" +COLUMN_ID+ " integer," + COLUMN_DATE + "VARCHAR," +
                            COLUMN_LOCATION+" VARCHAR," +COLUMN_TIME +" VARCHAR);";

将其更改为

private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_NAME +
                            " (" +COLUMN_ID+ " integer," + COLUMN_DATE + " VARCHAR,"  +
                            COLUMN_LOCATION+" VARCHAR," +COLUMN_TIME +" VARCHAR);";

注意空格:

COLUMN_DATE + "VARCHAR"

COLUMN_DATE + " VARCHAR,"

于 2012-11-30T05:16:22.170 回答
3

请更改数据库插入语句

private static final String DATABASE_INSERT = "INSERT INTO " +TABLE_NAME +  " (" +COLUMN_ID+ " ," + COLUMN_DATE + "," +
                                COLUMN_LOCATION+" ," +COLUMN_TIME +" )"  +
                                                " Values (47688507,'DEC-07-2012','MARVIN 203','20:00');";
于 2012-11-30T05:21:21.083 回答
0

尝试将您的数据库名称从“UPDdb_version6”更改为“UPDdb_version6.db”,即

private static final String DATABASE_NAME = "UPDdb_version6";

到,

 private static final String DATABASE_NAME = "UPDdb_version6.db";
于 2020-03-07T06:35:27.810 回答