gpt4 book ai didi

Android SQLite(一)没有这样的表

转载 作者:行者123 更新时间:2023-11-29 17:51:04 27 4
gpt4 key购买 nike

我是 android 编程的新手,这是我第一次尝试创建 sqlite 数据库。每次我调用 insert() 方法时,我都会收到错误消息“(1) 没有这样的表:songs.db”。我搜索了这个错误,但找不到任何解决方案。这是我的类(class):

public class HearOpenHandler extends SQLiteOpenHelper {

public static final String TABLE_SONGS = "songs";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_INTERPRET = "interpret";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_URI = "uri";

private static final String DATABASE_NAME = "songs.db";
private static final int DATABASE_VERSION = 2;

private static final String DATABASE_CREATE = "create table "
+ TABLE_SONGS + "("
+ COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_INTERPRET + " text not null, "
+ COLUMN_TITLE + " text not null, "
+ COLUMN_URI + " text not null);";

private static final String DATABASE_DROP = "drop table if exists " + TABLE_SONGS;

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


@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DATABASE_DROP);
onCreate(db);
}


public void insert(String interpret, String title, String uri) {

long rowId;

// Open database
SQLiteDatabase db = getWritableDatabase();

// Data which should be stored
ContentValues values = new ContentValues();
values.put(COLUMN_INTERPRET, interpret);
values.put(COLUMN_TITLE, title);
values.put(COLUMN_URI, uri);

// insert in database
rowId = db.insert(DATABASE_NAME, null, values);

if(rowId == -1)
Log.d(HearOpenHandler.class.getName(), "Error while inserting values into database");

}

}

最好的问候

最佳答案

这个

rowId = db.insert(DATABASE_NAME, null, values);

应该是

rowId = db.insert(TABLE_SONGS, null, values);

关于Android SQLite(一)没有这样的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22792150/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com