尝试将数据插入表中时出错

时间:2019-05-18 02:06:53

标签: java android sqlite

我正在使用Android Studio。我试图在SQLite中插入链接,但继续在boolean addData方法中出错。

我所有的导入/代码均正确无误。我只是不确定为什么会出现这些错误。我必须添加权限吗?

这是数据库方法的代码:

public class DataBase extends SQLiteOpenHelper {

private static final String TAG = "Database";
private static final String TABLE_NAME = "SongList_Table";
private static final String Column0 = "ID";
private static final String Column1 ="Music Link";


public DataBase(Context context){
    super(context, TABLE_NAME, null, 1);

}

public Cursor getData(){
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_NAME;
    Cursor data = db.rawQuery(query, null);
    return data;
}

public boolean addData(String item){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(Column1, item);

    Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
    long result = db.insert(TABLE_NAME, null, contentValues);
    if(result <0){
        return false;
    }
    else {
        return true;
    }
}
@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + Column1 + "TEXT)";
    db.execSQL(createTable);
}

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

}

错误:

  

D /数据库:addData:将https://www.soundhelix.com/audio-examples添加到SongList_Table   E / SQLiteLog:(1)在“链接”附近:语法错误   E / SQLiteDatabase:插入Music Link = https://www.soundhelix.com/audio-examples时出错       android.database.sqlite.SQLiteException:在“链接”附近:语法错误(代码1):,在编译时:INSERT INTO SongList_Table(Music Link)VALUES(?)           在android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法)           在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:898)           在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509)           在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)           在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)           在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31)           在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1500)           在android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1373)           在com.example.mobilecomputingcw2.DataBase.addData(DataBase.java:37)           在com.example.mobilecomputingcw2.MainActivity.dataAdd(MainActivity.java:69)           在com.example.mobilecomputingcw2.MainActivity $ 1.onClick(MainActivity.java:47)           在android.view.View.performClick(View.java:5675)           在android.view.View $ PerformClick.run(View.java:22651)           在android.os.Handler.handleCallback(Handler.java:836)           在android.os.Handler.dispatchMessage(Handler.java:103)           在android.os.Looper.loop(Looper.java:208)           在android.app.ActivityThread.main(ActivityThread.java:6304)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1063)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

编辑:即使在对Column1和createTable String进行更改之后,我仍然在将链接插入SQLite时遇到相同的错误。

Edit2:原来我只需要卸载应用程序。谢谢@forpas

编辑3:现在在getData方法上出现错误

2 个答案:

答案 0 :(得分:2)

那是因为"Music Link"中的空格。将其更改为下划线(或其他任何字符串)

...
private static final String Column1 ="Music_Link";
...

或在其周围加上引号

...
private static final String Column1 ="\"Music Link\"";
...

还有这里

...
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + Column1 + "TEXT)";
...

TEXT前缺少空格。应该是

String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + Column1 + " TEXT)";

答案 1 :(得分:0)

更改为:

private static final String Column1 ="[Music Link]";

然后从设备上卸载应用程序,以便删除数据库并重新运行以重新创建数据库。