我在编码方面很新,所以我复制粘贴一些教程的代码,但是当我试图删除“sleep”列中的所有数据时,显示此错误。
android.database.sqlite.SQLiteException:near“20170128”:语法错误 (代码1):,编译时:DELETE FROM 20170128 在android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native 方法) 在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) 在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31) 在android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1496) 在jm.myapplication.Sleep.sleep_repo.delete1(sleep_repo.java:51) 在jm.myapplication.Main.MainActivity $ 1.onClick(MainActivity.java:220) 在android.view.View.performClick(View.java:4780) 在android.view.View $ PerformClick.run(View.java:19866) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
这是我的代码。
这是我的主要活动
public void onClick(View v) {
final Integer newSleep = sleep;
sleep_repo repo = new sleep_repo(MainActivity.this);
others sleep = new others();
sleep.sleep = Recent_f_id;
sleep.sleep=newSleep;
repo.delete1(sleep);
Recent_f_id = repo.insert(sleep);
Toast.makeText(getApplicationContext(),
newSleep + " has been added", Toast.LENGTH_LONG)
.show();
d.dismiss();
}
虽然这是我的sleep_repo.java
public void delete1(others sleep) {
// db.delete(String tableName, String whereClause, String[] whereArgs);
// If whereClause is null, it will delete all rows.
SQLiteDatabase db = dbHelper.getWritableDatabase(); // helper is object extends SQLiteOpenHelper
db.delete(getDateTime(),null,null);
/*db.delete(getDateTime(),others.KEY_ID+"=?",new String[] { String.valueOf(sleep) });*/
db.close(); // Closing database connection
}
虽然这是我的others.java
public class others {
// Labels table name
public static final String TABLE = "others";
// Labels Table Columns names
public static final String KEY_ID = "id";
public static final String KEY_sleep = "sleep";
public static final String KEY_smoke = "smoke";
public static final String KEY_exercise = "exercise";
// property help us to keep data
public int Recent_f_id;
public Integer id;
public Integer sleep;
public Integer smoke;
public String exercise;
}
这是我的Dates.java
public class Dates extends SQLiteOpenHelper {
//version number to upgrade database version
//each time if you Add, Edit table, you need to change the
//version number.
private static final int DATABASE_VERSION = 4;
public static String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyyMMdd", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
// Database Name
private static final String DATABASE_NAME = getDateTime();
public Dates(Context context ) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//All necessary tables you like to create will create here
String CREATE_TABLE_meals = "CREATE TABLE " + meals.TABLE + "("
+ meals.KEY_breakfast + " TEXT, "
+ meals.KEY_lunch + " TEXT, "
+ meals.KEY_dinner + " TEXT, "
+ meals.KEY_other + " TEXT )";
String CREATE_TABLE_drinks = "CREATE TABLE " + drinks.TABLE + "("
+ drinks.KEY_alcoholic + " TEXT, "
+ drinks.KEY_water + " INTEGER )";
String CREATE_TABLE_others = "CREATE TABLE " + others.TABLE + "("
+ others.KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
+ others.KEY_sleep + " TEXT, "
+ others.KEY_smoke + " INTEGER, "
+ others.KEY_exercise + " INTEGER )";
db.execSQL(CREATE_TABLE_meals);
db.execSQL(CREATE_TABLE_drinks);
db.execSQL(CREATE_TABLE_others);
}
答案 0 :(得分:0)
20170128
不是有效的表名。 (除非用方括号或引号括起来,否则表名不能以数字开头)。