这是我的代码:
package com.example.user.sqliteapp2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText editName,editSurname,editMarks;
Button btnAddData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
editName=(EditText)findViewById(R.id.editText_name);
editSurname=(EditText)findViewById(R.id.editText_surname);
editMarks=(EditText)findViewById(R.id.editText_marks);
btnAddData=(Button) findViewById(R.id.button_add);
AddData();
}
public void AddData(){
btnAddData.setOnClickListener(
new View.OnClickListener(){
@Override
public void onClick(View v) {
Boolean inserted= myDb.insertData(editName.getText().toString(),
editSurname.getText().toString(),
editMarks.getText().toString());
if (inserted==true )
Toast.makeText(MainActivity.this,"DATAINSERTed",
Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"DATAINSERTed",
Toast.LENGTH_LONG).show();
}
}
);
}
编译应用程序后,我点击add data
按钮插入一些数据。然后它显示了这个致命的错误:
'[ 10-24 10:38:38.886 2439: 2439 D/ ]
HostConnection::get() New Host Connection established 0x7fc5fe207940, tid 2439
I/OpenGLRenderer: Initialized EGL, version 1.4
I/Choreographer: Skipped 42 frames! The application may be doing too much work on its main thread.
E/SQLiteLog: (1) near "table": syntax error
D/AndroidRuntime: Shutting down VM
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.sqliteapp2, PID: 2439
android.database.sqlite.SQLiteException: near "table": syntax error(code 1): -while compiling: create table student.table ( ID INTEGER PRIMARY KEY -#-AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER )at - android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnect# ion.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at com.example.user.sqliteapp2.DatabaseHelper.onCreate(DatabaseHelper.java:26)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.jav#a:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.j#ava:163)
at com.example.user.sqliteapp2.DatabaseHelper.insertData(DatabaseHelper.java:36)
at com.example.user.sqliteapp2.MainActivity$1.onClick(MainActivity.java:32)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)'
如何解决此问题?
答案 0 :(得分:0)
问题在于您的DatabaseHelper,您需要正确命名您的表。表名不能包含.
。
检查您的logcat输出android.database.sqlite.SQLiteException: near "table": syntax error(code 1): -while compiling: create table student.table
它表示您正在尝试创建一个名为student.table
的表无效。
查看此thread了解详情。