ssummeraw 2013-08-30 02:38 采纳率: 10%
浏览 4789
已采纳

如何在Sqlite数据库中插入数据列表?

我创建了一个简单的数据库用来插入数据库列表,但是当我想把数据发送到数据库时,有下面的错误:

08-28 15:30:43.015: ERROR/Database(822): Failure 1 (no such column: Sharma) on 0x276e50 when preparing 'INSERT INTO friends Values(Sharma,SaiGeetha,18);""

相关的代码:

sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
                ArrayList<String>FirstName = new ArrayList<String>();
                ArrayList<String>LastName = new ArrayList<String>();
                ArrayList<Integer >Age = new ArrayList<Integer>();
                   FirstName.add("SaiGeetha");

                   FirstName.add("Vivek");
                   FirstName.add("Rahul");
                   LastName.add("Sharma");

                   LastName.add("Lilani");
                   LastName.add("Lami");
                    Age.add(18);

                   Age.add(20);
                   Age.add(23);

                sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                        SAMPLE_TABLE_NAME +
                        " (LastName VARCHAR, FirstName VARCHAR," +
                        "  Age INT(3));");

                for(int i=0;i<3;i++)
                {
                    sampleDB.execSQL("INSERT INTO " +

    enter code here

                        SAMPLE_TABLE_NAME +
                            " Values("+LastName.get(i)+","+FirstName.get(i)+","+Age.get(i)+");"+"\"\"");
                }

                /*sampleDB.execSQL("INSERT INTO " +
                        SAMPLE_TABLE_NAME +
                        " Values ('Makam','Sai Geetha','India',25);");
                sampleDB.execSQL("INSERT INTO " +
                        SAMPLE_TABLE_NAME +
                        " Values ('Chittur','Raman','India',25);");
                sampleDB.execSQL("INSERT INTO " +
                        SAMPLE_TABLE_NAME +
                        " Values ('Solutions','Collabera','India',20);");*/

                Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM " +
                        SAMPLE_TABLE_NAME +
                        " where Age > 10 LIMIT 5", null);

                if (c != null ) {
                    if  (c.moveToFirst()) {
                        do {
                            String firstName = c.getString(c.getColumnIndex("FirstName"));
                            int age = c.getInt(c.getColumnIndex("Age"));
                            Log.e("---LIST FROM DATABASE--","---VALUE---"+firstName);
                            Log.e("---LIST FROM DATABASE--","---VALUE---"+age );

                        }while (c.moveToNext());
                    } 
                }
                sampleDB.close();

            } catch (SQLiteException se )
            {
                Log.e(getClass().getSimpleName(), "Could not create or Open the database");
            } finally {
                if (sampleDB != null) 
                    sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
                sampleDB.close();
            }
        }

哪里的问题呢?

  • 写回答

1条回答 默认 最新

  • Sunday_Vssupermadman 2013-09-04 03:35
    关注

    SAMPLE_TABLE_NAME +
    " Values("+LastName.get(i)+","+FirstName.get(i)+","+Age.get(i)+");
    其中在LastName.get(i)和FirstName.get(i)外面加上单引号
    变成如下:

    SAMPLE_TABLE_NAME +
    " Values('"+LastName.get(i)+"','"+FirstName.get(i)+"',"+Age.get(i)+");

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?