好吧,所以我一直在搜索这个问题而且我找到了一些我试过没有成功的答案。我注意到,如果我注释掉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;
}
}
}
这是活动:
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: #################################################################
编辑:我对课程进行了一些修改。
@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);");
}
这是在活动:
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: #################################################################
答案 0 :(得分:0)
更改此
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);");
注意:从列名称中删除空格。这是一个问题。如果没有解决,请告诉我。
答案 1 :(得分:-1)
我已经阅读了你的logcat,我发现
附近有语法错误“AUTOINCREMENT”:语法错误(代码1):,编译时:CREATE TABLE学生(ID INTEGER AUTOINCREMENT,BANNER ID INTEGER PRIMARY KEY,FIRST NAME TEXT,LAME NAME TEXT,GPA REAL,MAJOR TEXT);)
您的属性名称中有空格会删除这些空格或添加_而不是空格