2017-10-11 156 views
1

我一直在試圖從字符串數組添加一些項目到數據庫中,我似乎無法得到插入查詢工作我一直在工作了我看到here Ive嘗試了插入查詢的各種變體。數據沒有被插入到Sqlite數據庫使用插入查詢

這裏是我的代碼:

SQLiteDatabase db1 = openOrCreateDatabase("Station11.db", SQLiteDatabase.CREATE_IF_NECESSARY , null); 
    try{ 
     String query = "CREATE TABLE IF NOT EXISTS Station (" 
       + "Station_name VARCHAR);"; 
     db1.execSQL(query); 
     Toast.makeText(MainActivity.this, "Table created", Toast.LENGTH_LONG).show(); 
     for(i=0;i<10;i++) 
     { 
      Toast.makeText(MainActivity.this, stations[i][0],Toast.LENGTH_SHORT).show(); 
      query = "INSERT INTO Station VALUES ('"+stations[i][0]+"');"; 
      db1.execSQL(query); 

     } 
    }catch (Exception e){ 
     Toast.makeText(MainActivity.this, "An Error has occured", Toast.LENGTH_LONG).show(); 
    } 
    Cursor data_fetch = db1.rawQuery("Select Station_name From Station", null); 

    String[] station_array = new String[data_fetch.getCount()]; 
    data_fetch.moveToFirst(); 
    i = 0; 
    while (data_fetch.moveToNext()) { 
     String name = data_fetch.getString(data_fetch.getColumnIndex("Station_name")); 
     station_array[i] = name; 
     i++; 
     Toast.makeText(MainActivity.this, "retrieved data"+station_array[i], Toast.LENGTH_LONG).show(); 
    } 
    data_fetch.close(); 
} 

當我舉杯檢索的數據來敬酒說:retrieved datanull 我甚至嘗試插入一個字符串,而不是一個變量,但我仍然得到datanull作爲敬酒。

任何幫助,將不勝感激。

PS這是字符串數組IM試圖插入:

stations[0][0]= "New York"; 
    stations[1][0]= "Boston"; 
    stations[2][0]= "Las Vegas"; 
    stations[3][0]= "Miami"; 
    stations[4][0]= "Chicago"; 
    stations[5][0]= "New England"; 
    stations[6][0]= "Detroit"; 
    stations[7][0]= "Michigan"; 
    stations[8][0]= "Austin"; 
    stations[9][0]= "New Orealns"; 

回答

1

在你的代碼中插入查詢表名後缺少列名:

正確插入查詢:

INSERT INTO Station Station_name VALUES ('"+stations[i][0]+"'); 

例如:

INSERT INTO table_name_here (column1, column2, column3) VALUES ("Learn PHP", "John Poul", NOW()); 
+0

我試圖用'查詢插入= 「INSERT INTO站(STATION_NAME)VALUES( ' 」+站[I] [0] +「');」; db1.execSQL(query);'我仍然得到datanull作爲輸出 –

+0

請在沒有()'括號'的情況下編寫你的列名。 –

+0

你是指這樣的? ''INSERT INTO Station Station_name VALUES(''+ stations [i] [0] +'');「' –

0

我認爲,這個問題從使用二維數組的推導: -

stations[0][0]= "New York"; 
stations[1][0]= "Boston"; 
stations[2][0]= "Las Vegas"; 
stations[3][0]= "Miami"; 
stations[4][0]= "Chicago"; 
stations[5][0]= "New England"; 
stations[6][0]= "Detroit"; 
stations[7][0]= "Michigan"; 
stations[8][0]= "Austin"; 
stations[9][0]= "New Orealns"; 

而是請使用: -

String[] stations = new String[10]; 
    stations[0]= "New York"; 
    stations[1]= "Boston"; 
    stations[2]= "Las Vegas"; 
    stations[3]= "Miami"; 
    stations[4]= "Chicago"; 
    stations[5]= "New England"; 
    stations[6]= "Detroit"; 
    stations[7]= "Michigan"; 
    stations[8]= "Austin"; 
    stations[9]= "New Orealns"; 

或(大概這一個,因爲它是更靈活

String[] stations = new String[] { 
      "New York", 
      "Boston", 
      "Las Vegas", 
      "Miami", "Chicago", 
      "New England", 
      "Detroit", 
      "Michigan", 
      "Austin", 
      "New Orleans" 
    }; 

沿用: -

for(i=0;i< stations.length();i++) 
     { 
      Toast.makeText(MainActivity.this, stations[i],Toast.LENGTH_SHORT).show(); 
      query = "INSERT INTO Station VALUES ('"+stations[i]+"');"; 
      db1.execSQL(query); 

     } 

或者查詢可包括列名(S)和是: -

query = "INSERT INTO Station Station_name VALUES ('"+stations[i]+"');"; 

你一個完全工作版本代碼可能是: -

String[] stations = new String[] { 
      "New York", 
      "Boston", 
      "Las Vegas", 
      "Miami", "Chicago", 
      "New England", 
      "Detroit", 
      "Michigan", 
      "Austin", 
      "New Orleans" 
    }; 

    int i=0; 
    SQLiteDatabase db1 = openOrCreateDatabase("Station11.db", SQLiteDatabase.CREATE_IF_NECESSARY , null); 
    try{ 
     String query = "CREATE TABLE IF NOT EXISTS Station (" 
       + "Station_name VARCHAR);"; 
     db1.execSQL(query); 
     Log.d("STATION_TBLCRT","Table Created"); 
     //Toast.makeText(MainActivity.this, "Table created", Toast.LENGTH_LONG).show(); 
     for(i=0;i<stations.length;i++) 
     { 
      Log.d("STATION_INS","Inserting station " + stations[i]); 
      //Toast.makeText(MainActivity.this, stations[i],Toast.LENGTH_SHORT).show(); 
      query = "INSERT INTO Station VALUES ('"+stations[i]+"');"; 
      db1.execSQL(query); 

     } 
    }catch (Exception e){ 
     Log.d("STATION_INSERR","Error inserting station " + stations[i]); 
     //Toast.makeText(MainActivity.this, "An Error has occured", Toast.LENGTH_LONG).show(); 
    } 
    Cursor data_fetch = db1.rawQuery("Select Station_name From Station", null); 

    String[] station_array = new String[data_fetch.getCount()]; 
    //data_fetch.moveToFirst(); 
    i = 0; 
    while (data_fetch.moveToNext()) { 
     String name = data_fetch.getString(data_fetch.getColumnIndex("Station_name")); 
     station_array[i] = name; 
     Log.d("STATION_GET","Retrieved station " + station_array[i]); 
     i++; 
     //Toast.makeText(MainActivity.this, "retrieved data"+station_array[i], Toast.LENGTH_LONG).show(); 
    } 
    data_fetch.close(); 
} 

筆記!

  • 祝酒詞已被替換登錄
  • data_fetch.moveToFirst已被註釋掉,因爲這會導致跳過第一行。
  • 如果上述操作不止一次,則會添加更多套電臺。
  • data_fetch中的i ++已被移至報告站之後(否則數據總是爲空)。
  • 使用敬酒由於吐司的持續時間可以誤導,因此使用日誌,這樣你可以看到所有的結果。

輸出示例: -

10-14 08:16:37.446 22117-22117/mjt.sqlitedbexamples D/STATION_TBLCRT: Table Created 
10-14 08:16:37.446 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station New York 
10-14 08:16:37.451 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Boston 
10-14 08:16:37.454 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Las Vegas 
10-14 08:16:37.457 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Miami 
10-14 08:16:37.461 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Chicago 
10-14 08:16:37.465 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station New England 
10-14 08:16:37.468 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Detroit 
10-14 08:16:37.471 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Michigan 
10-14 08:16:37.473 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station Austin 
10-14 08:16:37.478 22117-22117/mjt.sqlitedbexamples D/STATION_INS: Inserting station New Orleans 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station New York 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Boston 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Las Vegas 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Miami 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Chicago 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station New England 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Detroit 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Michigan 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station Austin 
10-14 08:16:37.482 22117-22117/mjt.sqlitedbexamples D/STATION_GET: Retrieved station New Orleans