【发布时间】:2015-08-05 23:26:26
【问题描述】:
我是 Android Studio 的新手程序员,我的程序有一些问题,
我想在我的 SQLite 表中插入一些 DateTime 类型,但是当我想使用 ContentValue 插入它时,编译器会将其读取为错误语句。 它说
无法解析方法'put(java.lang.string,java.sql.date)
我不知道如何解决这个问题,即使我已经寻找了一些答案但仍然没有解决。
我在 Stackoverflow 上搜索并找到了一些答案,但它仍然无法解决我的错误,他们说要使用 DATETIME,但在这里我已经使用它但仍然没有解决我的错误。
请高手给个答案。 之前谢谢。
注意。这是我的代码
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String MyVillageSoftware = "MyVillageSoftware";
public static final String DATABASE_NAME = "Cashflow.db";
public static final String TABLE_Categ_NAME = "category_table";
public static final String TABLE_Trans_NAME = "transaction_table";
public static final String COL1 = "CategId";
public static final String COL2 = "CategName";
public static final String COL3 = "Note";
public static final String COL4 = "Currency";
public static final String COL5 = "_id";
//TOL for transaction Coloumn
public static final String TOL1 = "TransId";
public static final String TOL2 = "TransName";
public static final String TOL3 = "Amount";
public static final String TOL4 = "TransNote";
public static final String TOL5 = "TransDate";
public static final String TOL6 = "CategId";
public static final String TOL7 = "_id";
private static final String TAG = DatabaseHelper.class.getSimpleName();
SQLiteDatabase db;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 6);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table " + TABLE_Categ_NAME +
" (CategID Integer PRIMARY KEY AUTOINCREMENT, " +
"CategName Text," +
" Note Text," +
" Currency Text," +
" _id Text)");
db.execSQL("Create table " + TABLE_Trans_NAME +
" (TransID Integer PRIMARY KEY AUTOINCREMENT, " +
"TransName Text," +
" Amount Integer," +
" TransNote Text," +
" TransDate Datetime," +
" CategID Integer, " +
" _id Text)");
}
public boolean insertCategData(String categname, String note, String currency, String id){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, categname);
contentValues.put(COL3, note);
contentValues.put(COL4, currency);
contentValues.put(COL5, id);
long result = db.insert(TABLE_Categ_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
public boolean insertTransData (String transname, Integer amount, String transnote, Date transdate, Integer categid, String id){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(TOL1, transname);
contentValues.put(TOL2, amount);
contentValues.put(TOL3, transnote);
//PROBLEMS COME FROM HERE
contentValues.put(TOL4, transdate);
contentValues.put(TOL5, categid);
contentValues.put(TOL6, id);
long result = db.insert(TABLE_Categ_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
public List<String> getAllCategory() {
List<String> AllCategoryList = new ArrayList<String>();
String selectQuery = "SELECT * FROM " + TABLE_Categ_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
String ID = cursor.getString(0);
String Categ = cursor.getString(1);
String Note = cursor.getString(2);
String Curr = cursor.getString(3);
AllCategoryList.add(Categ);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return AllCategoryList;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Categ_NAME);
onCreate(db);
}
}
【问题讨论】:
-
但它仍然不能解决我的问题,先生。
标签: java android sqlite datetime