gpt4 book ai didi

android - 如何将条目插入到 SQLite 数据库中

转载 作者:行者123 更新时间:2023-11-30 03:57:16 24 4
gpt4 key购买 nike

我正在尝试从我的 View 中获取一些数据并在我的 Android 应用程序中进入我的 SQLite 数据库。

    SQLiteDatabase db = openOrCreateDatabase("SemesterDB", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Semesters (Session VARCHAR, YearStart INT," +
" MonthStart INT, DayStart INT,YearEnd INT, MonthEnd INT, DayEnd INT )");

//extract session from view
Spinner sessionSpinner = (Spinner) findViewById(R.id.sessionSpinner);
String session = (String) sessionSpinner.getSelectedItem();

//extract start date from view
DatePicker startDatePicker = (DatePicker) findViewById(R.id.semesterStartDate);
int startDay = startDatePicker.getDayOfMonth();
int startMonth = startDatePicker.getMonth();
int startYear = startDatePicker.getYear();

//extract end date from view
DatePicker endDatePicker = (DatePicker) findViewById(R.id.semesterEndDate);
int endDay = startDatePicker.getDayOfMonth();
int endMonth = startDatePicker.getMonth();
int endYear = startDatePicker.getYear();

String sessionName = (String) session+" "+startYear;

db.execSQL("INSERT INTO Semesters VALUES(sessionName,startYear,startMonth,startDay,endYear,endMonth,endDay);");
db.close();

--所以当我将值硬编码到 db.execSQL("INSERT..... 行时,它工作得很好但是当我尝试插入具有存储值的变量时,它没有。有什么想法吗?

这是用 eclipse juno 编写的 android 2.3 应用程序。

最佳答案

您的 db.execSQL 查询也可以对其进行一些修改。

    db.execSQL("INSERT INTO Semesters VALUES(" +
sessionName + "," +
startYear + "," +
startMonth + "," +
startDay + "," +
endYear + "," +
endMonth + ","
endDay + ");");

你做错了什么是在双引号中传递诸如 startYear 等变量,这些变量被视为字符串而不是它的值。所以你最终生成和执行的查询字符串结果是

INSERT INTO Semesters VALUES(sessionName,startYear,startMonth,startDay,endYear,endMonth,endDay);

因此它失败了,因为这不是有效的查询。

关于android - 如何将条目插入到 SQLite 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13144905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com