2012-04-02 81 views
0

這是一個可能是一個非常簡單的答案,但由於某種原因,沒有一個正確的解釋如何做到這一點,好了,所以我已經建立了我的表數據庫SQLite和現在我想輸入一些數據吧...創建SQLite表,如何輸入數據?

像這樣的事情,例如,其實就是這樣的:

CREATE TABLE `schedule` (
    `id_route` int(11) NOT NULL, 
    `id_stop` int(11) NOT NULL, 
    `time` time NOT NULL, 
    `days` varchar(25) NOT NULL 
) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

INSERT INTO `schedule` (`id_route`, `id_stop`, `time`, `days`) VALUES 
(1, 1, '05:00:00', 'all'), 
(1, 1, '06:00:00', 'all'), 
(1, 1, '09:35:00', 'all but weekdays'), 
(1, 2, '07:00:00', 'all'), 

你如何在android SQL中做到這一點?

public what definition?(){ 
ContentValues values3 = new ContentValues(); 
       values3.put(id_route, id_stop, "07.29", "day1"); 
       values3.put(id_route, id_stop, "07.41", "day1"); 
       values3.put(id_route, id_stop, "08.05", "day1"); 
       values3.put(id_route, id_stop, "08.12", "day1"); 
       values3.put(id_route, id_stop, "08.18", "day1"); 
       long id3 = db.insert(schedule table, null, values3); 
return? 
} 

回答

2
public void addContact() { 
    SQLiteDatabase db = this.getWritableDatabase();  
    ContentValues values = new ContentValues(); 
    values3.put(id_route, id_stop, "07.29", "day1"); 
      values3.put(id_route, id_stop, "07.41", "day1");    
    db.insert(TABLE_MENU, null, values); 
    db.close(); // Closing database connection 
} 
+0

我得到這個錯誤:put方法(字符串,字符串)在類型ContentValues不適用的參數(字符串,整數,字符串) – BoneStarr 2012-04-02 04:21:33

+1

你的錯誤告訴你問題。檢查參數的數量傳入...以及類型以及... – 2012-04-02 05:07:16

2

ContentValues.put語法就像放(鍵,值),其中關鍵是字段名。 所以,你應該插入的數據是這樣的:

ContentValues cv = new ContentValues(); 
cv.put("id_route", 1); 
cv.put("days", "day1"); 
db.insert("table1", null, cv); 

你可以閱讀有關ContentValues here

3

試試這個代碼創建並添加值到表中。我在這裏只是創建表並將值添加到表中。

Context context; 
private SQLiteDatabase db;  
private final String DB_NAME = "database_name"; 
private final int DB_VERSION = 1; 

private final String TABLE_NAME = "database_table"; 
private final String TABLE_ROW_ID = "id"; 
private final String TABLE_ROW_ONE = "table_row_one"; 
private final String TABLE_ROW_TWO = "table_row_two"; 

public DatabaseHelper(Context context) 
{ 
    this.context = context; 

    CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context); 
    this.db = helper.getWritableDatabase(); 
} 





public void addRow(String rowStringOne, String rowStringTwo) 
{ 

    ContentValues values = new ContentValues(); 
    values.put(TABLE_ROW_ONE, rowStringOne); 
    values.put(TABLE_ROW_TWO, rowStringTwo); 


    try{db.insert(TABLE_NAME, null, values);} 
    catch(Exception e) 
    { 
     Log.e("DB ERROR", e.toString()); 
     e.printStackTrace(); 
    } 
} 
+0

是否可以在這個示例中的values.put()中放置多於2個值? – BoneStarr 2012-04-02 04:39:27

+0

不,這是不可能的。你可以做連接,如 values.put(TABLE_ROW_ONE,「one」+「Two」); – wolverine 2012-04-02 04:42:14

+0

我看到,很抱歉,如果我沒有正確理解這一點,但我認爲這將被視爲一行:values3.put(id_route,id_stop,「07.29」,「day1」);並且該列將是相應的表:db.insert(TABLE_MENU,null,values); 如果是這種情況,那麼如何確保兩個以上的參數在同一行中? – BoneStarr 2012-04-02 11:45:10