在SQLiteOpenHelper中插入可以正常工作

时间:2014-05-15 03:02:50

标签: android sqlite

我在android中遇到SQLite的问题,我在表中尝试了一个简单的插入,但我有一个异常,在异常中我读到了这个:

INSERT INTO Biblio(Auteur,Image,Livre) VALUES (?,?,?)

这是我的方法:

public void AddBook(Bibliothèque bibliothèque)
{
    SQLiteDatabase db=this.getWritableDatabase();
    //this.open();
    ContentValues values=new ContentValues();
    values.put(KEY_Auteur,bibliothèque.getNomAuteur());
    values.put(KEY_Livre,bibliothèque.getNomLivre());
    values.put(Key_photo,bibliothèque.geturiimage().toString());
    db.insert("Biblio",null,values);
    db.close();

}

这是我的变量:

    private static final String TABLE_Nom = "Biblio";
    private static final String KEY_ID = "id";
    private static final String KEY_Auteur = "Auteur";
    private static final String KEY_Livre = "Livre";
    private static final String Key_photo="Image";

问题我不知道为什么但是db.insert反转了Beet之间的Image和Livre,因为我有一个例外,我不知道为什么我插入的值是? ? ?在调试中我可以看到值是正确的! 。正常的表达必须是:

INSERT INTO Biblio(Auteur,Livre,Image) VALUES (cc,test,(uri of picture))

这是完整的例外:

2927-2927/com.example.myapplication4.app E/SQLiteLog﹕ (1) table Biblio has no column named Livre
05-15 02:53:54.436    2927-2927/com.example.myapplication4.app E/SQLiteDatabase﹕ Error inserting Auteur=gsopfkgop Image=content://media/external/images/media/10 Livre=fhoksgkp
    android.database.sqlite.SQLiteException: table Biblio has no column named Livre (code 1): , while compiling: INSERT INTO Biblio(Auteur,Image,Livre) VALUES (?,?,?)

3 个答案:

答案 0 :(得分:2)

使用此查询创建表。

 db.execSQL
    ("CREATE TABLE Biblio (id INTEGER PRIMARY KEY AUTOINCREMENT ,Auteur TEXT,Livre TEXT,Image TEXT)");

然后您必须使用以下代码插入记录:

public void AddBook(Bibliothèque bibliothèque)
{
    SQLiteDatabase db=this.getWritableDatabase();
    //this.open();
    ContentValues values=new ContentValues();
    values.put(Auteur,bibliothèque.getNomAuteur());
    values.put(Livre,bibliothèque.getNomLivre());
    values.put(Image,bibliothèque.geturiimage().toString());
    db.insert("Biblio",null,values);
    db.close();

}

我希望它对你有用..

答案 1 :(得分:0)

2927-2927/com.example.myapplication4.app E/SQLiteLog﹕ (1) table Biblio has no column named Livre

您的表格没有名为Livre的列,您可能在表格创建代码中有拼写错误,或者您还没有定义名为Livre的列。

答案 2 :(得分:0)

您应该尝试重新安装您的应用吗? 此外,如果在测试设备中安装了应用程序后更改了db模式,则必须增加db version参数以调用onUpdate()方法或重新安装应用程序以调用onCreate()。