Table没有名为“Column Name”的列在SQLite中使用android

时间:2014-02-18 16:36:41

标签: 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;
    }

}

无法理解我哪里出错...

1 个答案:

答案 0 :(得分:2)

您忘记在列名ProfilePicUrl和列类型之间添加空格。在两者之间添加空格:

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

使用SQLiteQueryBuilder

避免此类错误的最佳选择