没有列名 - 在创建之后

时间:2013-07-22 10:59:10

标签: java android sqlite

我确信我做错了什么但是在花了很多时间之后失败了,找出了什么。简而言之,错误是某个列不存在但我在之前创建它:

static final String itemTable="ITEMS";
static final String colItemID="ItemID";
static final String colItemName="ItemName";
static final String colQuantity="ItemQuantity";
static final String colPrice="ItemPrice";
static final String colCategory="ItemCategory";
static final String colAisle="ItemAisle";
static final String colCreator="ItemCreator";
static final String colActMemID="ItemActivityMemberID";
static final String colDate="ItemDate";
static final String colHistory="ItemHistory";
static final String colPicture="ItemPicture"; 

create table命令:

try {
db.execSQL("CREATE TABLE " + itemTable + " " + "(" + colItemID + 
" INTEGER PRIMARY KEY AUTOINCREMENT, " + colItemName + " TEXT, " + 
colQuantity + " Integer, " + colPrice + "REAL, " + colCategory + " TEXT, " 
+ colAisle + " TEXT, " + colCreator + " TEXT, " + colActMemID + " TEXT, " + 
colDate + " TEXT, " + colHistory + " TEXT, " + colPicture + " TEXT)");
} catch (SQLException e) {
System.out.println(e);
}

然后,当我尝试创建视图时:

try {
    db.execSQL("CREATE VIEW "+viewList+
        " AS SELECT "+itemTable+"."+colItemID+" AS _id,"+
        " "+itemTable+"."+colItemName+","+
        " "+itemTable+"."+colPrice+","+
        " "+itemTable+"."+colQuantity+","+

        " "+membersTable+"."+colMemName+""+
        " FROM "+itemTable+" JOIN "+membersTable+
        " ON "+itemTable+"."+colActMemID+" ="+membersTable+"."+colMemID
        );
} catch (SQLException e) {
    // TODO Auto-generated catch block
    System.out.println(e);
}
他抱怨一个特定的专栏:

07-22 10:33:26.548: I/System.out(1753): back from db construct
07-22 10:33:26.628: E/SQLiteLog(1753): (1) no such column: ITEMS.ItemPrice
07-22 10:33:26.638: D/AndroidRuntime(1753): Shutting down VM
07-22 10:33:26.638: W/dalvikvm(1753): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-22 10:33:26.688: E/AndroidRuntime(1753): FATAL EXCEPTION: main
07-22 10:33:26.688: E/AndroidRuntime(1753): android.database.sqlite.SQLiteException: no such
column: ITEMS.ItemPrice (code 1): , while compiling: CREATE VIEW ViewList AS SELECT ITEMS.ItemID
AS _id, ITEMS.ItemName, ITEMS.ItemPrice, ITEMS.ItemQuantity, MEMBERS.MemberName FROM ITEMS JOIN
MEMBERS ON ITEMS.ItemActivityMemberID =MEMBERS.MemberID
07-22 10:33:26.688: E/AndroidRuntime(1753):     at 
android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-22 10:33:26.688: E/AndroidRuntime(1753):     at 
android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
07-22 10:33:26.688: E/AndroidRuntime(1753):     at 
android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
07-22 10:33:26.688: E/AndroidRuntime(1753):     at android.database.sqlite.SQLiteSession.prepare
(SQLiteSession.java:588)

我尽可能多地查看答案,但没有发现任何线索,我们将非常感谢您的帮助!

TNX,

半乳糖

1 个答案:

答案 0 :(得分:4)

您在colPrice后缺少空格字符,因此列名称将为ItemPriceREAL。改为

try {
   db.execSQL("CREATE TABLE " + itemTable + " " + "(" + colItemID + 
      " INTEGER PRIMARY KEY AUTOINCREMENT, " + colItemName + " TEXT, " + 
      colQuantity + " Integer, " + colPrice + " REAL, " + colCategory + " TEXT, " 
      + colAisle + " TEXT, " + colCreator + " TEXT, " + colActMemID + " TEXT, " + 
      colDate + " TEXT, " + colHistory + " TEXT, " + colPicture + " TEXT)");
} catch (SQLException e) {
   System.out.println(e);
}

通常,在构建String查询以避免SQL injection攻击时,最好避免SQL连接。在这种情况下,它(将查询作为单个String文字)会突出显示缺少的空格。