Android Java SQLite数据库:数据库不使用2个字符串输入



我正在学习代码,并制作了一个秒表,将延期保存在字符串中,并将日期作为字符串也作为字符串。我想将它们放在SQLite数据库中(因此,以后可以在listView中显示日期,并在另一个显示所有笔记本的活动中打开它。因此,我可能会在代码中查看一些内容。我已经评论了我认为我理解的部分,所以您可以遵循我的想法。

问题:当我按"保存"保存时执行以下代码并返回toastmessage:某种程度上出错。

 save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String dateStamp = getCurrentTimeStamp();
                AddData(dataInput, dateStamp);
                //DatabaseHelper.deleteAll();
            }
        }); //save data though AddData method as input the listText

方法adddata如下:

public void AddData(String time, String date) {
        boolean insertData = DatabaseHelper.addData(time, date);
        if (insertData) {
            toastMessage(dataInput);
        } else {
            toastMessage("Something went wrong");
        }
    }

数据库Helper类中的布尔方法是:

public boolean addData(String times, String date) { //addData that takes a string
        SQLiteDatabase db = this.getWritableDatabase(); //database called db and use getWritableDatabase method
        ContentValues contentValues = new ContentValues();  //make a new object of ContentValues
        contentValues.put(COL_2, times); //put COL_2 and the String in the ContentValues object
        contentValues.put(COL_3, date);
          long result = db.insert(TABLE_NAME, null, contentValues);   //insert contentValues object into the table
        //if date as inserted incorrectly it will return -1
        if (result == -1) {
            db.close();
            return false;
        } else {
            return true;
        }
    }

当我仅在AddData((中输入1个变量时,它起作用,但我后来实现了2个变量。我认为这应该起作用。下面我还放置了用于制作SQLite数据库的代码。

public static final String TABLE_NAME = "stopwatch";   //make a table with name
    public static final String COL_1 = "ID";   //make an ID for every colomn
    public static final String COL_2 = "times";    //make a 2nd colomn for data
    public static final String COL_3= "date";
    public DatabaseHelper(Context context) {
        super(context, TABLE_NAME, null, 1);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {   //make the onCreate method that takes the database as input
        String createTable = "CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COL_2 +" TEXT" + COL_3 +" TEXT)";    //create the table with SQL statements to input the data correctly
        db.execSQL(createTable);    //input the SQL statements in the DB
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  //make upgrade method that takes the database and the versions
        db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);   //execute SQL statements drop table and which one
        onCreate(db);   //run through create method
    }

我希望有人可以帮助我找到问题,以便我可以了解更多。

您的创建表查询缺少COL_2 +" TEXT"

之后的逗号
@Override
public void onCreate(SQLiteDatabase db) {   
    String createTable = "CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COL_2 +" TEXT, " + COL_3 +" TEXT)";  // Added a comma after the COL_2
    db.execSQL(createTable);   
}

我可以从快速浏览中看到一个问题,这是您的创建语句中缺少的逗号,这意味着您的数据库并非按照您的预期创建。尝试以下修正案。

String createTable = "CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COL_2 +" TEXT," + COL_3 +" TEXT)"; 

最新更新