我正在尝试使用 android 工作室制作一个项目,如以下视频所示: 源 1 和源 2 这是我添加的Java类中的代码:
package com.example.asus.androidsqlitedatabase_1;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by ASUS on 7/6/2017.
*/
public class Databasehelperclass extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Student.db";
public static final String TABLE_NAME = "Student_table";
public static final String COLUMN1= "ID";
public static final String COLUMN2 = "NAME";
public static final String COLUMN3 = "SURNAME";
public static final String COLUMN4 = "MARKS";
public Databasehelperclass(Context context) {
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("Create Table" + TABLE_NAME +
"(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1){
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(sqLiteDatabase);
}
}
这是我在主活动中的代码.java
package com.example.asus.androidsqlitedatabase_1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
Databasehelperclass mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new Databasehelperclass(this);
}
}
但是当我使用 genymotion 将应用程序执行到模拟器时,应用程序停止工作并且无法执行。请任何人帮助我!
问题:查询中的间距错误
解决方案:查询应采用以下格式:
sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_NAME + "("
+ COLUMN1 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN2 + " TEXT, "
+ COLUMN3 + " TEXT, "
+ COLUMN4 + " INTEGER, "
+ ")";
PS:最好在查询中使用变量名称而不是静态列名称。
希望对:)有所帮助