无法将数据插入SQLite表

时间:2011-11-10 07:52:00

标签: java android sqlite

我需要在表中插入一些值,但它给了我这个例外:

Android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x2e51a0

以下是我的代码:

public static final String DATABASE_NAVIGATION_CREATE = "create table navigation (KEY text,TYPE1 text,LINK_NAME text,DISPLAY_NAME text,FORWARD_ICON text,HIGHLIGHTED_ICON_WITH_TEXT text,ICON_WITHOUT_TEXT text,HIGHLIGHTED_ICON_WITHOUT_TEXT text,Banner_Image text,CHILDREN text,ICON_WITH_TEXT text)";

database.execSQL(DATABASE_NAVIGATION_CREATE);
private static final String INSERT_NAVIGATION = "insert into " 
                 + DATABASE_TABLE_NAVIGATION + "(KEY,TYPE1,LINK_NAME,DISPLAY_NAME,FORWARD_ICON,HIGHLIGHTED_ICON_WITH_TEXT,ICON_WITHOUT_TEXT,HIGHLIGHTED_ICON_WITHOUT_TEXT,Banner_Image,CHILDREN,ICON_WITH_TEXT)"+
                 " values (?,?,?,?,?,?,?,?,?,?,?)";

public WineDatabaseAdapter(Context context) {
    try{
       this.context = context;
       openHelper = new WineDatabaseHelper(context);
       this.db=openHelper.getWritableDatabase();
       this.insertStmt=this.db.compileStatement(INSERT_NAVIGATION);
       this.insertStmt=this.db.compileStatement(INSERT_ICON);
       this.insertStmt=this.db.compileStatement(INSERT);
    } catch(Exception e) {
       System.out.println(e);
    }
}

public long insertNavigation(String KEY ,String TYPE1,String LINK_NAME,String DISPLAY_NAME,String FORWARD_ICON,String HIGHLIGHTED_ICON_WITH_TEXT,String ICON_WITHOUT_TEXT,String HIGHLIGHTED_ICON_WITHOUT_TEXT,String Banner_Image,String CHILDREN,String ICON_WITH_TEXT ){
    this.insertStmt.bindString(1, KEY);
    this.insertStmt.bindString(2, TYPE1);
    this.insertStmt.bindString(3, LINK_NAME);

    this.insertStmt.bindString(4, DISPLAY_NAME);
    this.insertStmt.bindString(5, FORWARD_ICON);
    this.insertStmt.bindString(6, HIGHLIGHTED_ICON_WITH_TEXT);
    this.insertStmt.bindString(7, ICON_WITHOUT_TEXT);
    this.insertStmt.bindString(8, HIGHLIGHTED_ICON_WITHOUT_TEXT);
    this.insertStmt.bindString(9, Banner_Image);
    this.insertStmt.bindString(10, CHILDREN);
    this.insertStmt.bindString(11, ICON_WITH_TEXT);

    return this.insertStmt.executeInsert();
}


JSONObject objJson=navigation.getJSONObject(navigation_object);
key= navigation_object;

System.out.println(key+" this is the key");
try{
    display_name=objJson.getString("display_name");
    System.out.println(display_name);
} catch(Exception e){
    System.out.println(e);
}
try{ 
    type=objJson.getString("type");
    System.out.println("type is "+type);
} catch (Exception e) {
    System.out.println(e);
    e.printStackTrace();
}
try{
    banner_image=objJson.getString("banner_image");
    System.out.println(banner_image+"*********banner_image ************");
} catch(Exception e) {
    System.out.println(e);
}
try{
    link_name=objJson.getString("link_name");
    System.out.println("link_name**********"+link_name);
} catch(Exception e){
    System.out.println(e);
}
try{
    forward_icon=objJson.getString("forward_icon");
    System.out.println("forward_icon***********"+forward_icon);
} catch (Exception e) {
    System.out.println(e);
}
try{
    highlighted_icon_with_text= objJson.getString("highlighted_icon_with_text");
    System.out.println(highlighted_icon_with_text+"*****************highlighted_icon_with_text");
} catch (Exception e) {
    System.out.println(e);
}
try{
    children1= objJson.getString("children");
    System.out.println("children1"+children1+"******************");
} catch (Exception e) {
    System.out.println(e);
}
try{
    icon_without_text= objJson.getString("icon_without_text");
    System.out.println("icon_without_text*************"+icon_without_text);
} catch (Exception e) {
    System.out.println(e);
}
try{
 highlighted_icon_without_text=objJson.getString("highlighted_icon_without_text");
 System.out.println("highlighted_icon_without_text ***************** "+highlighted_icon_without_text);
} catch (Exception e) {
    System.out.println(e);
}
try{
    icon_with_text=objJson.getString("icon_with_text");
    System.out.println("icon_with_text******"+icon_with_text);
}catch(Exception e) {
    System.out.println(e);
}

db.insertNavigation(key, type, link_name, display_name, forward_icon, highlighted_icon_with_text, icon_without_text, highlighted_icon_without_text, banner_image, children1, icon_with_text);

2 个答案:

答案 0 :(得分:3)

这是一个很长的镜头,但我认为值得尝试。 “KEY”是SQL的特殊关键字,因此,请尝试将列名KEY重命名为create db&中的其他内容。插入脚本。

答案 1 :(得分:0)

确保您使用的是正确的字段名称。您可能会尝试在查询中使用字符串变量名称作为字段名称。

例:public final String DISPLAY_NAME="display_name";

您尝试将DISPLAY_NAME用作字段名称,而不是“display_name”。这将需要修改您的查询。 Android可能无法找到您在查询中指定的字段名称。