我开发了一个Android数据库应用程序 问题是,当我单击添加按钮时,会显示一条消息,表明已成功添加数据 但数据未插入数据库 然后我用SQLite查看器打开数据库,在那里我找不到我插入的任何数据......
注册页码:
package com.example.dbapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Register extends Activity {
Button add;
TextView b,c;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
add = (Button) findViewById(R.id.btnReg);
b = (TextView) findViewById(R.id.etName);
c = (TextView) findViewById(R.id.etOccu);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
String name = b.getText().toString();
String occ = c.getText().toString();
DataCon entry = new DataCon(Register.this);
entry.open();
entry.createEntry(name, occ);
entry.close();
Toast toast = Toast.makeText(getApplicationContext(), "Sucess", Toast.LENGTH_SHORT);
toast.show();
}
catch(Exception e) {
String err = e.toString();
Toast toast = Toast.makeText(getApplicationContext(), err, Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
数据库连接代码:
package com.example.dbapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DataCon {
public static final String rowid = "_id";
public static final String name = "persons_name";
public static final String hot = "persons_hotness";
private static final String DB_NAME = "myDB";
private static final String DB_TABLE = "tbl_me";
private static final int DB_VER = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DB_NAME, null, DB_VER);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DB_TABLE + " (" + rowid +
" INTEGER PRIMARY KEY AUTOINCREMENT, " + name + "TEXT NOT NULL, " +
hot + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " +DB_TABLE);
onCreate(db);
}
}
public DataCon(Context c) {
ourContext = c;
}
public DataCon open() {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public long createEntry(String name2, String occ) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(name, name2);
cv.put(hot, occ);
return ourDatabase.insert(DB_TABLE, null, cv);
}
}
现在我该如何解决这个问题?感谢
答案 0 :(得分:1)
你的SQL中有错误。您没有在列名称和类型之间提供空格。正确的应该是
db.execSQL("CREATE TABLE " + DB_TABLE + " (" + rowid +
" INTEGER PRIMARY KEY AUTOINCREMENT, " + name + " TEXT NOT NULL, " +
--------------------------- ^
hot + " TEXT NOT NULL);");`