我對如何DateTime.now在 SQLite 資料庫中插入感到困惑,我想使用并插入 DateTime,這是使用最多的示例,ContentValues但我想像SQLIteStatement下面的示例一樣插入,這可能嗎?
宣告資料庫存在日期時間在最后一列
sqLiteHelper.queryData("CREATE TABLE IF NOT EXISTS CgList(Id INTEGER PRIMARY KEY AUTOINCREMENT, cash_card_actual_no VARCHAR, hh_number VARCHAR,series_number VARCHAR, cc_image BLOB , id_image BLOB, cash_card_scanned_no VARCHAR , card_scanning_status VARCHAR, date_insert DATETIME DEFAULT CURRENT_TIMESTAMP)");
我想在插入資料時添加另一列 DateTime 它將自動保存日期。
public void insertScannedCashCard(String scannedCashCard,byte[] cc_image){
try {
SQLiteDatabase database = getWritableDatabase();
String sql = "INSERT INTO CgList VALUES (NULL,?,?,?,?,?,?,0)";
SQLiteStatement statement = database.compileStatement(sql);
statement.clearBindings();
statement.bindString(1, "");
statement.bindString(2, "");
statement.bindString(3, "");
statement.bindBlob(4, cc_image);
statement.bindString(5,"");
statement.bindString(6, scannedCashCard);
statement.executeInsert();
}
catch(Exception e){
Log.v(TAG,e.toString());
}
}
這是我試過的
public void insertScannedCashCard(String scannedCashCard,byte[] cc_image){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = sdf.format(new Date());
try {
SQLiteDatabase database = getWritableDatabase();
String sql = "INSERT INTO CgList VALUES (NULL,?,?,?,?,?,?,0,?)";
SQLiteStatement statement = database.compileStatement(sql);
statement.clearBindings();
statement.bindString(1, "");
statement.bindString(2, "");
statement.bindString(3, "");
statement.bindBlob(4, cc_image);
statement.bindString(5,"");
statement.bindString(6, scannedCashCard);
statement.bindDateeeeee(7, strDate); // don't know the syntax of date
statement.executeInsert();
}
catch(Exception e){
Log.v(TAG,e.toString());
}
}
uj5u.com熱心網友回復:
您可以使用:-
public void insertScannedCashCard(String scannedCashCard,byte[] cc_image){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = sdf.format(new Date());
try {
//SQLiteDatabase database = this.getWritableDatabase();
String sql = "INSERT INTO CgList VALUES (NULL,?,?,?,?,?,?,?,?)";
SQLiteStatement statement = database.compileStatement(sql);
statement.clearBindings();
statement.bindString(1, "");
statement.bindString(2, "");
statement.bindString(3, "");
statement.bindBlob(4, cc_image);
statement.bindString(5,"");
statement.bindString(6, scannedCashCard);
statement.bindString(7, strDate); // don't know the syntax of date
statement.executeInsert();
}
catch(Exception e){
Log.v(TAG,e.toString());
}
}
- 即日期是一個字串。
或者,如果您希望使用 DEFAULT 值,即 CURRENT_TIMESTAMP,則使用僅指定不使用默認值的列的插入(即省略列以使用定義的 DEFAULT)。
因此,要對id和date_insert列使用 DEFAULT 值,您可以使用:-
public void insertScannedCashCardV2(String scannedCashCard,byte[] cc_image){
try {
//SQLiteDatabase database = this.getWritableDatabase();
String sql = "INSERT INTO CgList (cash_card_actual_no,hh_number,series_number,cc_image,id_image,cash_card_scanned_no,card_scanning_status) VALUES (?,?,?,?,?,?,0)";
SQLiteStatement statement = database.compileStatement(sql);
statement.clearBindings();
statement.bindString(1, "");
statement.bindString(2, "");
statement.bindString(3, "");
statement.bindBlob(4, cc_image);
statement.bindString(5,"");
statement.bindString(6, scannedCashCard);
statement.executeInsert();
}
catch(Exception e){
Log.v(TAG,e.toString());
}
}
關于型別。SQLite 在處理列的方式上是獨一無二的,或者至少是不尋常的。型別親和力將是以下之一:-
- 文本
- 整數
- BLOB
- 真實的
- 數字
但是,您幾乎可以指定任何內容(適用一些限制),甚至什么都不指定,SQLite 將根據一組規則分配型別關聯。
SQLite has no specific date/time types but specifying DATETIME as a column type results in that column having the type affinity of NUMERIC according to the type affinity rules (i.e. the last catch-all rule is applied).
- e.g.
cash_card_actual_no VARCHARbecause the first rule that is matched is the 2nd rule:- - If the declared type of the column contains any of the strings "CHAR", "CLOB", or "TEXT" then that column has TEXT affinity. Notice that the type VARCHAR contains the string "CHAR" and is thus assigned TEXT affinity.
- you can even have a column type of rumplestiltskin, the rules drop through and the type affinity is the catch-all NUMERIC.
However, SQLite's flexibility allows, with one exception, any column to store any type of data. The exception is the special rowid column or an alias of the rowid column. An alias of the rowid column is where you specify INTEGER PRIMARY KEY (with or without AUTOINCREMENT), in which case the value MUST be an integer or NULL, In the case of NULL the value will be determined (automatically generated) by SQLite (typically 1 greater than the highest value, AUTOINCREMENT applies a further rule in that the value MUST be greater than the highest value that has ever been used)
- see
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/325072.html上一篇:在SQLite中檢查數字約束
下一篇:不能將null設定為唯一的列
