这可能是一个非常简单的答案,但由于某些原因没有人正确解释如何做到这一点,所以我已经在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?
}
答案 0 :(得分: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();
}
}
答案 1 :(得分: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
}
答案 2 :(得分:2)
ContentValues.put语法类似于put(key,value),其中key是字段名称。 所以你应该插入这样的数据:
ContentValues cv = new ContentValues();
cv.put("id_route", 1);
cv.put("days", "day1");
db.insert("table1", null, cv);
您可以阅读有关ContentValues here
的信息