0

I'm already trying the database function in first project. There is nothing wrong in first project. after that, im try with another project, but the result is Sqlite Returned: error code = 1, msg = no such table: Login .. im already checking the code from the previous project, everything is same but i have no idea why is it happend.

tool that i used is Eclipse Juno.

MainActivity.java this is function that insert the data.

LoginEntity LoginE = new LoginEntity(LoginID, LoginPassword);
    LoginDA login = new LoginDA(this);
    login.open();
    login.createLogin(LoginE);

LoginEntity.java this file is the class for Login (setter and getter)

public class LoginEntity {
private String LoginID;
private String LoginPassword;

public LoginEntity(){

}

public LoginEntity(String LoginID, String LoginPassword) {
    this.LoginID = LoginID;
    this.LoginPassword = LoginPassword;
}

public String getLoginID() {
    return LoginID;
}
public void setLoginID(String LoginID) {
    this.LoginID = LoginID;
}
public String getLoginPassword() {
    return LoginPassword ;
}
public void setLoginPassword(String LoginPassword) {
    this.LoginPassword = LoginPassword;
}

LoginDA.java Database handler

public class LoginDA {
private static final String LoginID = "LoginID";
private static final String LoginPassword = "LoginPassword";

private static final String DATABASE_NAME = "LecturerStudentAppointment";
public static final String DATABASE_TABLE = "Login";
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 + " (" +
                LoginID + " INTEGER PRIMARY, " +
                LoginPassword + " 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 LoginDA(Context c){
    ourContext =c;
}
public LoginDA open() throws SQLException{
    ourHelper = new DbHelper(ourContext);

    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}
public void close(){

}
public long createLogin(LoginEntity LoginEntity) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(LoginID, LoginEntity.getLoginID());
    cv.put(LoginPassword, LoginEntity.getLoginPassword());
    return ourDatabase.insert(DATABASE_TABLE, null, cv);

}
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
Daisy
  • 19
  • 4
  • Are you sure your table name is Login? Or LoginEntity? – NerosE Nov 16 '14 at 15:21
  • yes.. _DATABASE_TABLE = "Login";_ LoginEntity as a class(constructor,setter and getter) ..or I have a mistake in declaring the database? – Daisy Nov 16 '14 at 15:23

3 Answers3

0

A primary key is specified with PRIMARY KEY, not PRIMARY.

Also see When is SQLiteOpenHelper onCreate() / onUpgrade() run?; you need to delete you app's old data.

Community
  • 1
  • 1
CL.
  • 173,858
  • 17
  • 217
  • 259
0

change the db.execSQL as:

=> remove the semicolon(;) from the SQL string.

db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                LoginID + " INTEGER PRIMARY KEY, " +
                LoginPassword + " TEXT NOT NULL"+")");

it will be a better practice, if you write the SQL Query String in seperate and pass it inside execSQL();

Eg :

String SQL_QUERY = "CREATE TABLE " + DATABASE_TABLE + " (" +
                LoginID + " INTEGER PRIMARY KEY, "
                + LoginPassword + " TEXT NOT NULL"+")";

db.execSQL(SQL_QUERY);
Blue_Alien
  • 2,148
  • 2
  • 25
  • 29
  • The semicolon is optional and does not hurt. Why is using a separate variable a better practice? – CL. Nov 19 '14 at 07:51
-1

Is there any other table depending on 'Login'? If so, I think the error could be when you execute the DROP statement. Try to use CASCADE