I am trying to add another table to my database, the first one was successfully created, and the data was also inserted. but when I tried to add another table, I inserted the data ... I got this error (there is no such table when compiling INSERT to .... etc.
I searched on google and here also on stackoverflow, I found some guys with codes similar to mine. someone had to add (onUpgrade method) to their code so that it would automatically update the database version .... but I already wrote this. I'm so confused. any help please .. ??
this is the database code:
package group.com; import android.content.ContentValues; import android.content.Context; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class DBAdapter { public static final String KEY_Admin_ROWID = "_id"; public static final String KEY_Admin_fullName = "adminFullName"; public static final String KEY_Admin_UserName = "AdminUserName"; public static final String KEY_Admin_Password = "AdminPassword"; private static final String TAG = "DBAdapter"; private static final String DATABASE_NAME = "RamiTariq"; private static final String DATABASE_TABLE = "info"; private static final String DATABASE_TABLE_Admin = "admin"; private static final int DATABASE_VERSION = 1; private static final String DATABASE_CREATE = "create table " + DATABASE_TABLE + " (" + KEY_ROWID + " integer primary key autoincrement, " + KEY_NAME + " text not null, " + KEY_PSSWORD + " text not null , " + KEY_F_NAME + " text not null ," + KEY_DATE + " text not null," + KEY_JOB + " text not null," + KEY_ADDRESS + " text not null," + KEY_PHONE + " text not null," + KEY_NATIONAL_NUMBER + " text not null," + KEY_MONEY_SHOULD_PAY + " text null," + KEY_ACTUAL_MONEY + " text null);"; private static final String DATABASE_CREATE_ADMIN = "create table " + DATABASE_TABLE_Admin + " (" + KEY_Admin_ROWID + " integer primary key autoincrement, " + KEY_Admin_fullName + " text not null, " + KEY_Admin_UserName + " text not null, " + KEY_Admin_Password + " text not null);"; private final Context context; private DatabaseHelper DBHelper; private SQLiteDatabase db; public DBAdapter(Context ctx) { this.context = ctx; DBHelper = new DatabaseHelper(context); } private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } public void onCreate(SQLiteDatabase db) { try { db.execSQL(DATABASE_CREATE); db.execSQL(DATABASE_CREATE_ADMIN); } catch (SQLException e) { e.printStackTrace(); } } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE); db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE_Admin); onCreate(db); } }
and this is the activity code:
package group.com; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class InsertAdmin extends Activity { DBAdapter db = new DBAdapter(this); EditText fullName, username, password, rePassword; Button insertAdmin; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.insertadmin); fullName = (EditText) findViewById(R.id.fullName); username = (EditText) findViewById(R.id.username); password = (EditText) findViewById(R.id.password); rePassword = (EditText) findViewById(R.id.rePassword); insertAdmin = (Button) findViewById(R.id.insertAdmin); insertAdmin.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { String fName = fullName.getText().toString(); String uName = username.getText().toString(); String pass = password.getText().toString(); String rePass = rePassword.getText().toString(); if(fName.length()==0 || uName.length()==0 || pass.length()==0 || rePass.length()==0) { displayMessage("Please Enter Full Data"); return; } if (uName.length() == 0) { displayMessage("Enter username"); return; } if (pass.length() == 0) { displayMessage("Enter The password"); return; } if(pass.equals(rePass)) { db.open(); long id = db.insertAdmin(fName, uName, pass); if(id>0) { displayMessage(id + "\nSuccessfuly Inserted"); } else { displayMessage("Not Inserted"); } db.close(); startActivity(new Intent(getBaseContext(),Admin.class)) ; } else displayMessage("Your Passwords doesn't match"); } }); } public void displayMessage(String msg) { Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } }