android - 表在带有 android 的 SQLite 中没有名为 "Column Name"的列

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

}

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

最佳答案

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

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

避免此类错误的最佳选择是使用 SQLiteQueryBuilder

关于android - 表在带有 android 的 SQLite 中没有名为 "Column Name"的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21859895/

相关文章:

android - 提取联系人的照片

java - 安卓 - SQLiteException : near "=": syntax error (code 1)

Django:如何在 sqlite3 数据库中添加/删除字段?

c# - 为什么这段代码只循环两次?

android - 合并两个库

Android - 创建自定义启动器

java - 如何在任务管理器中隐藏应用程序的预览

sql - 查找两列的最佳组合

asp.net - .NET ASP Core Docker 容器中的 Sqlite 数据库为空

android - Android studio 未能完成Gradle 执行,出现致命异常?