我正在尝试写入数据库。我编写了所有代码并创建了一个按钮,当我按下它时,我在EditTexts中写入的数据应该添加到数据库中。但是当我按下按钮将数据添加到数据库时,应用程序崩溃了。
这是主要活动代码:
public class MainActivity extends AppCompatActivity {
Button button1;
database database1;
Button button2, SendData;
EditText editNOH, Hint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database1 = new database(this);
button1 =(Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button4);
SendData = (Button)findViewById(R.id.button5);
editNOH = (EditText)findViewById(R.id.editText3);
Hint = (EditText)findViewById(R.id.editText2);
AddData();
viewAll();
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Main2Activity.class));
}
});}
public void viewAll(){
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = database1.getData();
if (res.getCount() == 0){
showMessage("Error", "Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()){
buffer.append("NOQ:" + res.getString(0) + "\n");
buffer.append("SENTENCE:" + res.getString(1)+ "\n\n");
}
showMessage("Data", buffer.toString());
}
});}
public void showMessage(String title, String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
public void AddData(){
SendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = database1.insertData(editNOH.getText().toString(), Hint.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();
}
}
});
}
}
这是数据库代码:
public class database extends SQLiteOpenHelper {
public static final String Name = "help_answer.db";
public static final String tName = "hints_table";
public static final String NOQ = "NOQ;
public static final String helpSentence = "SENTENCE";
public database(Context context) {
super(context, Name, null, 1);
}
int a=0;//global
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + tName + " (NOQ INTEGER PRIMARY KEY AUTOINCREMENT,, SENTENCE TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + tName);
onCreate(db);
}
public boolean insertData(String number, String helpS ){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues ContentValues = new ContentValues();
ContentValues.put(NOQ,number);
ContentValues.put(helpSentence, helpS);
long result = db.insert(tName, null, ContentValues);
if(result == -1){
return false;
}
else{
return true;
}
}
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from"+ tName, null);
return res;
}
}
这是logcat:
android.database.sqlite.SQLiteException: table hints_table has no column named SENTENCE (code 1): , while compiling: INSERT INTO hints_table(NQO,SENTENCE) VALUES (?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.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.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at com.example.ejhon.puzzlegame.database.insertData(database.java:33)
at com.example.ejhon.puzzlegame.MainActivity$3.onClick(MainActivity.java:70)
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 :(得分:1)
这是因为你没有在表名和表关键字之间留出空格。
这里,
tablehints_table
像这样更改代码,
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + tName + " (NUMBER_OF_QUESTION INTEGER PRIMARY KEY AUTOINCREMENT, SENTENCE TEXT)");
}
并从表的列名中删除空格。
尝试以上代码。希望这会有所帮助。
答案 1 :(得分:0)
在命令中添加空格。改变这个
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table" + tName + "(NUMBER OF QUESTION INTEGER PRIMARY KEY AUTOINCREMENT, SENTENCE TEXT)");
}
到这个
int i=0;//global
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + tName + " ("+i+" INTEGER PRIMARY KEY AUTOINCREMENT,"+NOQ+" SENTENCE TEXT);");
}