程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-如何在Sqlite數據庫中插入數據列表?

android-如何在Sqlite數據庫中插入數據列表?

編輯:編程綜合問答
如何在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();
            }
        }

哪裡的問題呢?

最佳回答:


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)+");

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved