[英]Android Studio - SQL database/table not creating - app won't open
但是,在Android Studio中创建SQL数据库时,该应用程序将无法在模拟器上打开,并且在使用设备监视器时在/ data / data / package / databases /上找不到数据库文件。
我正在研究api_21模拟器,我认为它应该支持sqlite。 最终,我将使用数据库信息来通过按钮执行各种功能,但到目前为止,这些功能已被忽略。
请帮忙!
MainActivity.java
package edu.tamu.thecaddyapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button buttonDriver, buttonFouriron, buttonFiveiron, buttonSixiron, buttonSeveniron, buttonEightiron, buttonNineiron, buttonPW, buttonFiftysix, buttonSW, buttonSixty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteHelper db = new SQLiteHelper(this);
db.getWritableDatabase();
buttonDriver = findViewById(R.id.button_DRIVER);
buttonFouriron = findViewById(R.id.button_4IRON);
buttonFiveiron = findViewById(R.id.button_5IRON);
buttonSixiron = findViewById(R.id.button_6IRON);
buttonSeveniron = findViewById(R.id.button_7IRON);
buttonEightiron = findViewById(R.id.button_8IRON);
buttonNineiron = findViewById(R.id.button_9IRON);
buttonPW = findViewById(R.id.button_PW);
buttonSW = findViewById(R.id.button_SW);
buttonFiftysix = findViewById(R.id.button_56DEG);
buttonSixty = findViewById(R.id.button_60DEG);
}
public void buttonDriver(View view) {
}
public void buttonFouriron(View view) {
}
public void buttonFiveiron(View view) {
}
public void buttonSixiron(View view) {
}
public void buttonSeveniron(View view) {
}
public void buttonEightiron(View view) {
}
public void buttonNineiron(View view) {
}
public void buttonPW(View view) {
}
public void buttonSW(View view) {
}
public void buttonFiftysix(View view) {
}
public void buttonSixty(View view) {
}
}
SQLiteHelper.java
package edu.tamu.thecaddyapp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class SQLiteHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "SQLiteDatabase.db";
public static final String TABLE_NAME = "golf_averages";
public static final String _id = "_id";
public static final String COL_1 = "COL_1";
public static final String COL_2 = "COL_2";
public static final String COL_3 = "COL_3";
public static final String COL_4 = "COL_4";
public static final String COL_5 = "COL_5";
public static final String COL_6 = "COL_6";
public static final String COL_7 = "COL_7";
public static final String COL_8 = "COL_8";
public static final String COL_9 = "COL_9";
public static final String COL_10 = "COL_10";
public String create_table = "CREATE TABLE" + TABLE_NAME
+ " (_id integer primary key autoincrement, "
+ COL_1 + "text, "
+ COL_2 + "text, "
+ COL_3 + "text, "
+ COL_4 + "text, "
+ COL_5 + "text, "
+ COL_6 + "text, "
+ COL_7 + "text, "
+ COL_8 + "text, "
+ COL_9 + "text, "
+ COL_10 + "text" +")";
public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("SQLiteHelper", "Context Reached");
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(create_table);
Log.d("onCreate", "onCreate reached");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS golf_averages");
onCreate(sqLiteDatabase);
}
}
您必须在SQL语句中添加空格。 您有以下几行:
+ COL_9 + "text, "
但COL_9
定义为:
public static final String COL_9 = "COL_9";
表示SQL语句在编译时以COL_9结尾,并在名称中加入text,表示行名称为COL_9text
。
在编写text
之前添加一个空格,它将解决该问题。 但是,第一部分(带有ID)并不存在此问题,它似乎仅针对文本列。
此外,在SQL语句的开头也会发生相同的问题:
"CREATE TABLE" + TABLE_NAME
在TABLE
单词后面添加一个空格。
发现这种情况的最简单方法是在logcat中查找错误。 这将向您显示SQL语法错误的发生位置,并有助于缩小需要进行的更改。
请在查询中添加适当的间距
public String create_table = "CREATE TABLE " + TABLE_NAME
+ " (_id integer primary key autoincrement, "
+ COL_1 + " text, "
+ COL_2 + " text, "
+ COL_3 + " text, "
+ COL_4 + " text, "
+ COL_5 + " text, "
+ COL_6 + " text, "
+ COL_7 + " text, "
+ COL_8 + " text, "
+ COL_9 + " text, "
+ COL_10 + " text" +")";
间距在SQL查询中非常重要
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.