我正在尝试通过函数在sqllite中插入日期。
问题 我面临的问题是日期未插入表中并且在logcat中显示错误。 的码 数据库代码
package com.example.lenovo1.tabhostbrowser;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class databasehelper extends SQLiteOpenHelper {
private static int DATABASE_VERSION =9;
public static final String DATABASE_NAME = "browse6.db";
public static final String TABLE_NAME = "bookmark";
public static final String COLUMN_ID = "ID";
public static final String COLUMN_title = "Title";
public static final String COLUMN_url = "Url";
public static final String COLUMN_creatd = "created at";
public databasehelper(Context context) {
super(context, DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " ( " + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_title + " TEXT, " + COLUMN_url + " TEXT," + COLUMN_creatd + "DATETIME );");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
}
public void addContact(databasemodel d) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_title, d.gettitle()); // Contact Name
values.put(COLUMN_url, d.getURL()); // Contact Phone
values.put(COLUMN_creatd,d.getDateTime());
// Inserting Row
db.insert(TABLE_NAME, null, values);
db.close(); // Closing database connection
}
public ArrayList<databasemodel> getAllContacts() {
ArrayList<databasemodel> contactList = new ArrayList<databasemodel>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
databasemodel structure = new databasemodel();
structure.setid(Integer.parseInt(cursor.getString(0)));
structure.setTitle(cursor.getString(1));
structure.setURL(cursor.getString(2));
structure.setTIME(cursor.getString(3));
// Adding contact to list
contactList.add(structure);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
}
getdate函数的代码是
public String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"E, MMM dd yyyy hh:mm:ss a", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
记录错误
04-30 14:29:02.075 2454-2454/? E/SQLiteDatabase: Error inserting created at=Sun, Apr 30 2017 02:29:02 PM Title=Google Url=https://www.google.co.in/webhp
android.database.sqlite.SQLiteException: near "at": syntax error (code 1): , while compiling: INSERT INTO bookmark(created at,Title,Url) VALUES (?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1472)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
at com.example.lenovo1.tabhostbrowser.databasehelper.addContact(databasehelper.java:56)
at com.example.lenovo1.tabhostbrowser.browser$2$3.onClick(browser.java:158)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:2)
表名中有空格,导致语法错误:
public static final String COLUMN_creatd = "created at";
您可以使用下划线或删除created和at之间的空格。您可以修改将其指定为:
public static final String COLUMN_creatd = "created_at";
如果您想保留空白使用 []
:
public static final String COLUMN_creatd = "[created at]";
希望这有帮助。