Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.database.sqlite.SQLITeException: no such table: admin while compiling: INSERT INTO ...etc

i am trying to add another table in my database , the first one was created successfully, and the data was inserted successfully too. but when i tried to add another table, then inserted the data...i got this error (no such table while compiling INSERT into....etc.

i searched in google and here too in stackoverflow, i found some guys with codes similar to mine. someone had to add (onUpgrade method) to his code so it will autoincrement the database version.... but i have already wrote it. i am 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);
    }
}

// ---opens the database---
public DBAdapter open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}

// ---closes the database---
public void close() {
    DBHelper.close();
}


//------insert an admin into the database-----
public long insertAdmin(String full_name, String user_name, String password ) {
    ContentValues initialValues = new ContentValues();

    initialValues.put(KEY_Admin_fullName, full_name);
    initialValues.put(KEY_Admin_UserName, user_name);
    initialValues.put(KEY_Admin_Password,password);

    return db.insert(DATABASE_TABLE_Admin, null,initialValues);
}


  }

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();
}

  }
like image 240
Rami Rasikh Avatar asked Jan 12 '14 00:01

Rami Rasikh


1 Answers

Uninstall your app so that the old version of the database is deleted.

Your database version is still 1 so the onUpgrade() isn't run. The database file already exists so onCreate() isn't run. The already existing database file doesn't contain all the tables you create in your current version of onCreate().

like image 94
laalto Avatar answered Nov 15 '22 11:11

laalto