SQLite 数据库使我的应用程序崩溃
SQLite database crashing my app
我正在尝试使用 android 工作室制作一个项目,如以下视频所示:
Source 1 and Source 2
这是我添加的代码 java class :
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将应用程序执行到模拟器时,应用程序停止工作并且无法执行。请任何人帮助我!!
问题: 查询中的间距错误
解决方法:查询应该是这样的格式:
sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_NAME + "("
+ COLUMN1 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN2 + " TEXT, "
+ COLUMN3 + " TEXT, "
+ COLUMN4 + " INTEGER, "
+ ")";
PS: 最好在查询中使用变量名而不是静态列名。
希望对您有所帮助:)
我正在尝试使用 android 工作室制作一个项目,如以下视频所示: Source 1 and Source 2 这是我添加的代码 java class :
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将应用程序执行到模拟器时,应用程序停止工作并且无法执行。请任何人帮助我!!
问题: 查询中的间距错误
解决方法:查询应该是这样的格式:
sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_NAME + "("
+ COLUMN1 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN2 + " TEXT, "
+ COLUMN3 + " TEXT, "
+ COLUMN4 + " INTEGER, "
+ ")";
PS: 最好在查询中使用变量名而不是静态列名。
希望对您有所帮助:)