java - 无法在 Android Studio 上将 DateTime 插入 SQLite

标签 java android sqlite datetime

我是 Android Studio 的新手程序员,我的程序存在一些问题,

我想将一些 DateTime 类型插入到我的 SQLite 表中,但是当我想使用 ContentValue 插入它时,编译器将其读取为错误语句。 它说

Can't resolve method '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);
    }
}

最佳答案

SQLite数据库仅支持整数、文本和其他一些基本数据类型。不支持 Date 对象,但您始终可以将 Date 对象保存为长时间戳,采用 Date.getTime() long 值并将其添加为整数类型。并且您不应该使用 ContentValues.put() 方法,而应使用 putInt、putString 和类似方法。在您的情况下,在表中定义一个日期整数列,并通过 putLong() 方法将其放入您的 ContentValue 对象中。

关于java - 无法在 Android Studio 上将 DateTime 插入 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31844437/

相关文章:

java - 如何使用 Vertx SQL 接口(interface)合并数据库查询结果

java - 在 Codename One 应用程序中使用 SQLite

c# - 使用 SQLite 和 C# 错误 "Insufficient parameters supplied to the command"的 Dapper

c - 我应该如何调整 SQLite 以获得最小延迟?

java - 如何将应用程序从 Tomcat8 映射到 JBoss Web 服务器中的 Apache?

java - 我正在 java 中旋转图像,但想保存旋转图像

java - 如何通过引用它的子节点从 firebase 中删除一个节点?

android - Flutter - 如何减少设备上的应用程序大小

java - 如何设置 RadioGroupFieldEditor 的默认值

android - Ionic 应用程序图标在 android 中没有变化