gpt4 book ai didi

Android 应用程序使用 getWritableDatabase 崩溃

转载 作者:行者123 更新时间:2023-12-02 06:48:01 27 4
gpt4 key购买 nike

好吧,所以我一直在搜索这个问题,我找到了几个我尝试过但没有成功的答案。我注意到,如果我注释掉 getWritableDatabase,应用程序不会崩溃,但它不会完成我想要的操作。

我还引用了这个video帮助我创建类

这是我的 SQLHelper 类:

package com.example.amasio.testapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by Amasio on 11/9/15.
*/
public class DatabaseController extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "Student.db";
public static final String TABLE_NAME = "Student";
public static final String COL_1 = "ID";
public static final String COL_2 = "BANNER ID";
public static final String COL_3 = "FIRST NAME";
public static final String COL_4 = "LAST NAME";
public static final String COL_5 = "GPA";
public static final String COL_6 = "MAJOR";

public DatabaseController(Context context) {
super(context, DATABASE_NAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER AUTOINCREMENT, BANNER ID INTEGER PRIMARY KEY, " +
"FIRST NAME TEXT, LAST NAME TEXT, GPA REAL, MAJOR TEXT);");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}

public boolean insertStudent(int bannerId, String firstName, String lastName, double gpa, String major){

SQLiteDatabase db = this.getWritableDatabase();

ContentValues cv = new ContentValues();
cv.put(COL_2, bannerId);
cv.put(COL_3, firstName);
cv.put(COL_4, lastName);
cv.put(COL_5, gpa);
cv.put(COL_6, major);
long result = db.insert(TABLE_NAME, null, cv);
db.close();

if(result == -1){
return false;
} else{
return true;
}

}
}

这是 Activity :

package com.example.amasio.testapplication;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class SignupActivity extends AppCompatActivity {

DatabaseController studentDb;
Student s;
EditText bannerIdText;
EditText firstNameText;
EditText lastNameText;
//add email EditText
EditText gpaText;
Spinner majorSpinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Sign Up");
setSupportActionBar(toolbar);

bannerIdText = (EditText) findViewById(R.id.bannerId);
firstNameText = (EditText) findViewById(R.id.firstName);
lastNameText = (EditText) findViewById(R.id.lastName);
gpaText = (EditText) findViewById(R.id.gpa);
majorSpinner = (Spinner) findViewById(R.id.major);

// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.college_majors, android.R.layout.simple_spinner_item);

// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// Apply the adapter to the spinner
majorSpinner.setAdapter(adapter);
}

public void onRegisterClicked(View v){

int banner = Integer.parseInt(bannerIdText.getText().toString());
double gpa = Double.parseDouble(gpaText.getText().toString());
String majorValue = majorSpinner.getSelectedItem().toString();

studentDb = new DatabaseController(this);
boolean inserted = studentDb.insertStudent(banner, firstNameText.getText().toString(),
lastNameText.getText().toString(), gpa, majorValue);

if(inserted){
Toast.makeText(SignupActivity.this, "Data Inserted", Toast.LENGTH_LONG ).show();
}else {
Toast.makeText(SignupActivity.this, "Data not Inserted", Toast.LENGTH_LONG).show();
}
}

}

这是日志:

11-10 00:24:56.781 21455-21455/? E/SQLiteLog: (1) near "AUTOINCREMENT": syntax error
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: FATAL EXCEPTION: main
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: Process: com.example.amasio.testapplication, PID: 21455
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method of the activity
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at android.view.View$1.onClick(View.java:4298)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at android.view.View.performClick(View.java:5254)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21179)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6837)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at android.view.View$1.onClick(View.java:4293)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at android.view.View.performClick(View.java:5254) 
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21179) 
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739) 
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95) 
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145) 
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6837) 
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372) 
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: Caused by: android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error (code 1): , while compiling: CREATE TABLE Student(ID INTEGER AUTOINCREMENT, BANNER ID INTEGER PRIMARY KEY, FIRST NAME TEXT, LAST NAME TEXT, GPA REAL, MAJOR TEXT);
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: #################################################################
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: Error Code : 1 (SQLITE_ERROR)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: Caused By : SQL(query) error or missing database.
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: (near "AUTOINCREMENT": syntax error (code 1): , while compiling: CREATE TABLE Student(ID INTEGER AUTOINCREMENT, BANNER ID INTEGER PRIMARY KEY, FIRST NAME TEXT, LAST NAME TEXT, GPA REAL, MAJOR TEXT);)
11-10 00:24:56.791 21455-21455/? E/AndroidRuntime: #################################################################

编辑:我对类(class)做了一些更改。

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID AUTOINCREMENT, BANNER_ID INTEGER PRIMARY KEY, " +
"FIRST_NAME TEXT, LAST_NAME TEXT, GPA DOUBLE, MAJOR TEXT);");

}

这是在 Activity 中:

public void onRegisterClicked(View v){

int banner = Integer.parseInt(bannerIdText.getText().toString());
double gpa = Double.parseDouble(gpaText.getText().toString());
String majorValue = majorSpinner.getSelectedItem().toString();

studentDb = new DatabaseController(this);
boolean inserted = studentDb.insertStudent(banner, firstNameText.getText().toString(),
lastNameText.getText().toString(), gpa, majorValue);

if(inserted){
Toast.makeText(SignupActivity.this, "Data Inserted", Toast.LENGTH_LONG ).show();
}else {
Toast.makeText(SignupActivity.this, "Data not Inserted", Toast.LENGTH_LONG).show();
}
}

日志:

11-10 12:06:04.631 31358-31393/? E/MTPJNIInterface: noti = 29
11-10 12:06:05.291 29884-29884/? E/SQLiteLog: (1) near "NAME": syntax error
11-10 12:06:05.301 29884-29884/? E/SQLiteDatabase: Error inserting FIRST NAME=mike LAST NAME=page BANNER ID=950266678 MAJOR=Computer Science GPA=3.88
11-10 12:06:05.301 29884-29884/? E/SQLiteDatabase: android.database.sqlite.SQLiteException: near "NAME": syntax error (code 1): , while compiling: INSERT INTO Student(FIRST NAME,LAST NAME,BANNER ID,MAJOR,GPA) VALUES (?,?,?,?,?)
11-10 12:06:05.301 29884-29884/? E/SQLiteDatabase: #################################################################
11-10 12:06:05.301 29884-29884/? E/SQLiteDatabase: Error Code : 1 (SQLITE_ERROR)
11-10 12:06:05.301 29884-29884/? E/SQLiteDatabase: Caused By : SQL(query) error or missing database.
11-10 12:06:05.301 29884-29884/? E/SQLiteDatabase: (near "NAME": syntax error (code 1): , while compiling: INSERT INTO Student(FIRST NAME,LAST NAME,BANNER ID,MAJOR,GPA) VALUES (?,?,?,?,?))
11-10 12:06:05.301 29884-29884/? E/SQLiteDatabase: #################################################################

最佳答案

改变这个

db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER AUTOINCREMENT, BANNER ID INTEGER PRIMARY KEY, " +
"FIRST NAME TEXT, LAST NAME TEXT, GPA REAL, MAJOR TEXT);");

db.execSQL("CREATE TABLE " + TABLE_NAME + "( ID INTEGER AUTOINCREMENT, BANNER_ID INTEGER PRIMARY KEY, " +
"FIRST_NAME TEXT, LAST_NAME TEXT, GPA_REAL, MAJOR TEXT);");

注意:删除列名称中的空格。这是一个问题。如果没有解决请告诉我。

关于Android 应用程序使用 getWritableDatabase 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33623510/

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