我正在尝试使用android studio创建一个项目,如以下视频: Source 1和Source 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);
}
}
这是我在MainActivity.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将应用程序执行到模拟器时,应用程序停止工作并且无法执行。请有人帮帮我!!
答案 0 :(得分:1)
问题:查询中的间距错误
解决方案:查询应采用以下格式:
sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_NAME + "("
+ COLUMN1 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN2 + " TEXT, "
+ COLUMN3 + " TEXT, "
+ COLUMN4 + " INTEGER, "
+ ")";
PS:最好在Query中使用变量名而不是静态列名。
希望有所帮助:)