developer developer - 3 months ago 12
Android Question

android.database.sqlite.SQLiteException: no such column: personname: , while compiling: SELECT id, personname FROM numbersTable

im a new in android,I want get data in db to textview.

but it has error I cannot correct this problem.

Please help me.

This is DB class

public class blockornot1 {
String TAG;
private dbHelper1 ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabese;

public final static String DATABASE_NAME1="databasename.db";
public final String DATABASE_TABLE="numbersTable";
public final String KEY_NAME="personname";
public final String KEY_ID="id";
public final int DATABASE_VERSION =2;


public class dbHelper1 extends SQLiteOpenHelper{

public dbHelper1 (Context context) {
super (context,DATABASE_NAME1,null,DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+ DATABASE_TABLE+ "("+KEY_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_NAME + "TEXT NOT NULL);");
Log.v(TAG, "creat");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);

}

}``

public blockornot1(Context c){

ourContext = c;
}

public blockornot1 open() throws SQLException{
ourHelper= new dbHelper1(ourContext);
ourDatabese= ourHelper.getWritableDatabase();
return this;
}

public void close(){
ourHelper.close();

}

public long creatEntry(String inputtext) {
// TODO Auto-generated method stub
ContentValues cv= new ContentValues();
cv.put(KEY_NAME, inputtext);
Log.v(inputtext, "add to database");
return ourDatabese.insert(DATABASE_TABLE, null, cv);

}

public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ID, KEY_NAME };
Cursor c=ourDatabese.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ID);
int iName = c.getColumnIndex(KEY_NAME);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){

result= result + c.getString(iRow) + " " + c.getString(iName) + "/n";
}
return result;
}


}


and this is my main class

public class Mainclass extends Activity implements OnClickListener {
EditText et;
String TAG;
public void onCreate(Bundle savedInstanceState) {
Log.v(TAG,"onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

et=(EditText)findViewById(R.id.edittext1);
Button bt=(Button)findViewById(R.id.button1);
Button bt1=(Button)findViewById(R.id.button2view);
bt.setOnClickListener(this);
bt1.setOnClickListener(this);

}

public void onClick(View v) {

switch (v.getId()) {
case R.id.button1:
boolean dididwork =true;
try{
String inputtext=et.getText().toString();
blockornot1 entry= new blockornot1(Mainclass.this);
entry.open();
entry.creatEntry(inputtext);
entry.close();
Log.v(TAG," okkkkkkk hastaaaaaaa");
break;}
catch (Exception e){
String error=e.toString();
Dialog d= new Dialog(this);
d.setTitle("Heack ya!");
TextView t= new TextView(this);
t.setText(error);
d.setContentView(t);
d.show();
dididwork=false;

}finally {
if(dididwork){
Dialog d= new Dialog(this);
d.setTitle("Heack ya!");
TextView t= new TextView(this);
t.setText("success");
d.setContentView(t);
d.show();

}

}

case R.id.button2view:
Intent i = new Intent("android.intent.action.SQLVIEW");
startActivity(i);
break;

}




}


}

and this class is for display DB in textview

public class ShowDb extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
TextView tv=(TextView)findViewById(R.id.textViewsqlview);
blockornot1 info= new blockornot1(this);
info.open();
String data= info.getData();
info.close();
tv.setText(data);
}


}

Sam Sam
Answer

I'm guessing that you changed the table's create statement at some point, but until you increment the database version your app will not automatically implement any changes. Try setting the DATABASE_VERSION to 3.

Addition

If you are going to bind the information in your database to a ListView, Spinner, etc then you should change KEY_ID to _id. (Android is particular about the name of the primary key column.)