我正在使用sqllite进行CRUD操作。但是当我要运行该程序时会显示以下错误。这里我给出了我的databasehelper.java文件& mainactivity.java文件代码。
这是我的DatabaseHelper.java文件代码。
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Student.db";
public static final String TABLE_NAME = "Student_Table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "SURNAME";
public static final String COL_4 = "SUBJECT";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, NAME VARCHAR (20) NOT NULL, SURNAME VARCHAR (20) NOT NULL, SUBJECT VARCHAR (20) NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String name,String surname,String subject){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,subject);
long result = db.insert(TABLE_NAME,null,contentValues);
if (result == -1)
return false;
else
return true;
}
}
这是我的MainActivity.java文件
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,editSubject;
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);
editSubject = (EditText)findViewById(R.id.editText_Subject);
btnAddData = (Button)findViewById(R.id.button_add);
AddData();
}
public void AddData(){
btnAddData.setOnClickListener(
new View.OnClickListener(){
@Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(editName.getText().toString(),editSurname.getText().toString(),editSubject.getText().toString());
if (isInserted == true)
Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data Not Inserted",Toast.LENGTH_LONG).show();
}
}
);
}
}
当我要保存数据时,它显示以下消息
“不幸的是,SQlLiteApp已停止......”
答案 0 :(得分:0)
从您的日志中: android.database.sqlite.SQLiteException:near“TABLEStudent_Table”:语法错误(代码1) 使用以下内容:简而言之,在表格之后和存在之后添加空格
db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, NAME VARCHAR (20) NOT NULL, SURNAME VARCHAR (20) NOT NULL, SUBJECT VARCHAR (20) NOT NULL);");
升级时:
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME +";");
答案 1 :(得分:0)
您似乎忘记在CREATE TABLE
和EXISTS
之后添加空格,因此更改
"CREATE TABLE" + TABLE_NAME + "(ID
到
"CREATE TABLE " + TABLE_NAME + " (ID
和
"DROP TABLE IF EXISTS" + TABLE_NAME
到
"DROP TABLE IF EXISTS " + TABLE_NAME
注意:请在重试前卸载应用。