2014-02-18 41 views
1

我正在写一个android应用程序,并试图将我的数据存储在数据库。 我如何以往得到的错误:表没有名为“列名”的列在SQLite与android

“表Friend_Master没有名为Profile_Pic_Url列”

我已经通过这种所有的职位了,并试图“卸载并重新安装”和“清除数据“,但没有奏效。

这里是我的代码:

package com.example.groupchat; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class MyDatabase extends SQLiteOpenHelper{ 

    //Definition of the database 
    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_NAME = "AppInformation"; 

    //Definition of the table 
    private static final String TABLE_NAME = "Friend_Master" ; 

    //Definition of the fields 
    private static final String ID = "F_ID"; 
    static final String FirstName = "FIRST_NAME"; 
    private static final String LastName = "LAST_NAME"; 
    private static final String DisplayName = "DISPLAY_NAME"; 
    private static final String ProfilePicUrl = "Profile_Pic_Url"; 

    public MyDatabase(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); //Testing purpose ONLY 
     String CreateTable = "CREATE TABLE " + TABLE_NAME + "(" + ID + " TEXT PRIMARY KEY , " + 
       FirstName + " TEXT, " + LastName + " TEXT, " + DisplayName + " TEXT, "+ ProfilePicUrl + "TEXT" + ");" ; 
     db.execSQL(CreateTable); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
     onCreate(db); 
    } 

    public void insertInformationInDatabase(FriendInformation info){ 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues cv = new ContentValues(); 
     cv.put(ID, info.getID()); 
     cv.put(FirstName, info.getFirst_Name()); 
     cv.put(LastName, info.getLast_Name()); 
     cv.put(DisplayName, info.getDisplay_Name()); 
     cv.put(ProfilePicUrl, info.getProfile_Pic_Url()); 
     db.insert(TABLE_NAME, null, cv); 
     db.close(); 
    } 

    public FriendInformation getInformationFromDatabase(String _ID, String First_Name){ 
     SQLiteDatabase db=this.getReadableDatabase(); 
     String Query = "SELECT * FROM " + TABLE_NAME + " WHERE " + ID + " = \"" + _ID + "\" AND " + FirstName + " = \"" + First_Name + "\""; 
     Cursor cursor = db.rawQuery(Query, null); 
     if (cursor.moveToFirst()){ 
      cursor.moveToFirst(); 
      FriendInformation info = new FriendInformation(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4)); 
      return info; 
     } 
     return null; 
    } 

    public FriendInformation getInformationFromDatabase(String Display_Name){ 
     SQLiteDatabase db=this.getReadableDatabase(); 
     String Query = "SELECT * FROM " + TABLE_NAME + " WHERE " + DisplayName + " = \"" + Display_Name + "\""; 
     Cursor cursor = db.rawQuery(Query, null); 
     if (cursor.moveToFirst()){ 
      cursor.moveToFirst(); 
      FriendInformation info = new FriendInformation(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4)); 
      return info; 
     } 
     return null; 
    } 

} 

能不明白的地方,我错了......

+4

使用'ProfilePicUrl + “TEXT”',而不是'ProfilePicUrl + “TEXT”'在创建表查询 –

回答

2

你忘了列名ProfilePicUrl和列类型之间添加空间。作为双方之间添加空格:

String CreateTable = "CREATE TABLE " + TABLE_NAME + 
         "(" + ID + " TEXT PRIMARY KEY , " + 
        FirstName + " TEXT, " + LastName + " TEXT, " 
        + DisplayName + " TEXT, "+ ProfilePicUrl + " TEXT " + ");" ; 

最好的选择,以避免这种类型的错误使用SQLiteQueryBuilder

相关问题