gpt4 book ai didi

java - Android Studio - SQL 数据库/表未创建 - 应用程序无法打开

转载 作者:行者123 更新时间:2023-12-01 17:53:44 30 4
gpt4 key购买 nike

正在 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 和名称中的文本结尾,表示行名称为 COL_9text

在写入文本之前添加一个空格即可解决此问题。不过,问题不存在于第一部分(带有 ID),它似乎仅适用于文本列。

此外,同样的问题出现在SQL语句的开头:

"CREATE TABLE" + TABLE_NAME

在单词TABLE后添加一个空格。

发现这种情况发生的最简单方法是在 logcat 中查找错误。这会向您显示 SQL 语法错误发生的位置,并有助于缩小您需要进行的更改的范围。

关于java - Android Studio - SQL 数据库/表未创建 - 应用程序无法打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47279601/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com