我在android中创建了一个数据库,当我在其中添加一些数据时,我试图看到我添加的内容是空的 这是数据库的类
package tt.aa.xx;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class Dbinfo {
public static final String KEY_ROWID="_ID";
public static final String KEY_NAME="person_name";
public static final String KEY_SCALERATING="person_rate";
public static final String KEY_AGE="person_age";
private static final String DATABASE_NAME="SignDB";
private static final String DATABASE_TABLE="peopleTable";
private static final int DATABASE_VERSION=1;
private Dbhelper ourHelper;
public 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_AGE+" TEXT NOT NULL, "+KEY_SCALERATING+" TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXIESTS"+DATABASE_TABLE);
onCreate(db);
}
}
public Dbinfo(Context c)
{
ourcontext=c;
}
public Dbinfo open()throws SQLException
{
ourHelper=new Dbhelper(ourcontext);
ourDatabase=ourHelper.getWritableDatabase();
return this;
}
public void close()
{
ourHelper.close();
}
public long create(String nameS, String ratingScaleS,String ageS) {
// TODO Auto-generated method stub
ContentValues cv=new ContentValues();
cv.put(KEY_NAME,nameS);
cv.put(KEY_SCALERATING, ratingScaleS);
cv.put(KEY_AGE, ageS);
return ourDatabase.insert(DATABASE_TABLE,null,cv);
}
public String getdata() {
// TODO Auto-generated method stub
String []columns=new String[]{KEY_ROWID,KEY_NAME,KEY_AGE,KEY_SCALERATING};
String result="";
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
int iRow=c.getColumnIndex(KEY_ROWID);
int iName=c.getColumnIndex(KEY_NAME);
int iage=c.getColumnIndex(KEY_AGE);
int iScale=c.getColumnIndex(KEY_SCALERATING);
for(c.moveToFirst();c.moveToLast();c.moveToNext())
{
result=result+c.getString(iRow)+c.getString(iName)+c.getString(iage)+c.getString(iScale)+'\n';
}
return result;
}
}
这是xml活动的类
package tt.aa.xx;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class DatabaseActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
EditText Name,RatingScale,age;
Button Update_sqlDB,view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intialze();
}
private void intialze() {
// TODO Auto-generated method stub
Name=(EditText)findViewById(R.id.NameED);
age=(EditText)findViewById(R.id.ageET);
RatingScale=(EditText)findViewById(R.id.ratingscaleET);
Update_sqlDB=(Button)findViewById(R.id.updateSQLDB_BT);
view=(Button)findViewById(R.id.viewBT);
Update_sqlDB.setOnClickListener(this);
view.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.updateSQLDB_BT:
Toast.makeText(this, "hsh", Toast.LENGTH_SHORT).show();
boolean diditwork=true;
try
{String NameS=Name.getText().toString();
String RatingScaleS=RatingScale.getText().toString();
String ageS=age.getText().toString();
Dbinfo info=new Dbinfo(this);
info.open();
info.create(NameS,RatingScaleS,ageS);
info.close();
}
catch (Exception e)
{
String error=e.toString();
Dialog d=new Dialog(this);
d.setTitle("Error");
TextView tt=new TextView(this);
tt.setText("Erro in saving");
d.setContentView(tt);
d.show();
}
finally
{
if(diditwork)
{
Dialog d=new Dialog(this);
d.setTitle("OHH YEAAH");
TextView tv=new TextView(this);
tv.setText("sucess");
d.setContentView(tv);
d.show();
}
}
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromInputMethod(age.getWindowToken(),0);
break;
case R.id.viewBT:
Intent i=new Intent("tt.aa.xx.VIEWDB");
startActivity(i);
break;
}
}
}
我创建了一个最终的Activity来查看数据库的内容
package tt.aa.xx;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ViewDb extends Activity {
TextView sqlinfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
sqlinfo=(TextView)findViewById(R.id.SQLinfoTV);
Dbinfo infos=new Dbinfo(this);
infos.open();
String data=infos.getdata();
sqlinfo.setText(data);
}
}
答案 0 :(得分:1)
我仔细阅读,你的数据库助手DbInfo看起来很好。
我认为问题可以在你的getData()方法中找到。
for(c.moveToFirst();c.moveToLast();c.moveToNext())
{
result=result+c.getString(iRow)+c.getString(iName)+c.getString(iage)+c.getString(iScale)+'\n';
}
这里用作for循环cursor.moveToLast()的条件,当它成功地将光标移动到最后一个条目时返回true。
用.c.isAfterLast()替换c.moveToLast。如果没有其他错误,您应该得到结果。
答案 1 :(得分:0)
在处理数据库时,你必须关心你的创作。
1.Check database is created or not?
2.Check table is created or not?
3.if first and second correct then you have to takecare about CRUD operation.
This对您和最有效的演示有帮助。