我一直在制作一个在线教程中的应用程序,在其中我正在制作一个用于存储女孩性感的数据库。目前,它必须能够存储数据并查看数据。一切都很好,只是当我显示每个条目时,属于它们的行id都是NULL。我试着把它固定了两天,但到目前为止我还没有运气。我还是一个新手,有经验的人能告诉我如何解决这个问题吗?
主要活动如下:
public class SQLiteExample extends Activity implements OnClickListener {
Button sqlUpdate, sqlView, sqlModify, sqlGetInfo, sqlDelete;
EditText sqlName, sqlHotness, sqlRow;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqliteexample);
initiate();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bSQLUpdate:
boolean didItWork = true;
try{
String name = sqlName.getText().toString();
String hotness = sqlHotness.getText().toString();
HotOrNot entry = new HotOrNot(SQLiteExample.this);
entry.open();
entry.createEntry(name, hotness);
entry.close();
}catch(Exception e){
didItWork = false;
}finally{
if (didItWork){
print("YESSSS", "it's working!");
}
else{
print("NOOOOO", "Something went wrong");
}
}
break;
case R.id.bSQLOpenView:
Intent openView = new Intent(SQLiteExample.this, SQLView.class);
startActivity(openView);
return;
case R.id.bgetInfo:
String s = sqlRow.getText().toString();
long l = Long.parseLong(s);
HotOrNot hon = new HotOrNot(this);
hon.open();
String returnedName = hon.getName(l);
String returnedHotness = hon.getHotness(l);
hon.close();
sqlName.setText(returnedName);
sqlHotness.setText(returnedHotness);
break;
}
}
private void initiate() {
sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
sqlView = (Button) findViewById(R.id.bSQLOpenView);
sqlModify = (Button) findViewById(R.id.bSQLmodify);
sqlGetInfo = (Button) findViewById(R.id.bgetInfo);
sqlDelete = (Button) findViewById(R.id.bSQLdelete);
sqlName = (EditText) findViewById(R.id.etSQLName);
sqlHotness = (EditText) findViewById(R.id.etSQLHotness);
sqlRow = (EditText) findViewById(R.id.etSQLrowInfo);
sqlView.setOnClickListener(this);
sqlUpdate.setOnClickListener(this);
sqlModify.setOnClickListener(this);
sqlGetInfo.setOnClickListener(this);
sqlDelete.setOnClickListener(this);
}
public void print(String title, String message){
Dialog d = new Dialog(this);
d.setTitle(title);
TextView tv = new TextView(this);
tv.setText(message);
d.setContentView(tv);
d.show();
};
}
数据库类:
public class HotOrNot {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_HOTNESS = "persons_hotness";
private static final String DATABASE_NAME = "HotOrNotdb";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY_KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_HOTNESS + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public HotOrNot(Context c){
ourContext = c;
}
public HotOrNot open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String name, String hotness) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_HOTNESS, hotness);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iHotness = c.getColumnIndex(KEY_HOTNESS);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iHotness) + "n";
}
return result;
}
}
查看活动:
public class SQLView extends Activity implements OnClickListener{
TextView tv;
Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
initiate();
HotOrNot info = new HotOrNot(this);
info.open();
String data = info.getData();
info.close();
tv.setText(data);
}
private void initiate(){
tv = (TextView) findViewById(R.id.tvSQLInfo);
back = (Button) findViewById(R.id.bBack);
back.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent back = new Intent(SQLView.this, SQLiteExample.class);
startActivity(back);
return;
};
}
提前感谢!
存在SQL语法错误。更改
KEY_ROWID + " INTEGER PRIMARY_KEY AUTOINCREMENT, "
至
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
即使下划线成为空格。
卸载您的应用程序,以便再次调用onCreate()
。由于您并没有看到有关语法的错误,所以此版本的SQL并没有运行。