dimanche 12 juillet 2015

android.database.sqlite.SQLiteException: no such table: users_table (code 1): , while compiling: INSERT INTO users_table

So i saw and read all the same questions,tried to solve the problem, but i didn't find the correct answer. I tried to modify the VERSION NUMBER, ran the app, but i've got that error.After that i removed the app from my phone, changed the VERSION NUMBER to 1,run the app again...same error.

Here is my code:

   MySQLite sql; 
Context context; 

public MySQLiteAdapter(Context context) { //constructor 
sql = new MySQLite(context); 
this.context=context; 
} 

public long insertUser(String Username,String Pass, String Name, String Mail){ 
SQLiteDatabase db=sql.getWritableDatabase(); 

ContentValues cv=new ContentValues(); 

cv.put(MySQLite.COLUMN_USERNAME,Username); 
cv.put(MySQLite.COLUMN_PASSWORD,Pass); 
cv.put(MySQLite.COLUMN_REALNAME,Name); 
cv.put(MySQLite.COLUMN_MAIL,Mail); 

long id=db.insert(MySQLite.TABLE_USERS,null,cv); 
return id; 
} 

class MySQLite extends SQLiteOpenHelper { 

private static final String DATABASE_NAME = "APPLICATION"; //name database 

private static final int DATABASE_VERSION = 1; 

public static final String TABLE_USERS="users_table"; 
public static final String COLUMN_ID = "_id"; 

public static final String COLUMN_USERNAME="USERNAME"; 
public static final String COLUMN_PASSWORD="PASSWORD"; 
public static final String COLUMN_REALNAME="NAME"; 
public static final String COLUMN_MAIL="EMAIL"; 

private static final String TABLE_CREATE_USERS="CREATE TABLE "+TABLE_USERS+"("+COLUMN_ID 
+"INTEGER PRIMARY KEY AUTOINCREMENT, " 
+COLUMN_USERNAME+"VARCHAR(250) NOT NULL, "+COLUMN_PASSWORD+"VARCHAR(250) NOT NULL, " 
+COLUMN_REALNAME+"VARCHAR(250) NOT NULL, "+COLUMN_MAIL+"VARCHAR(250) NOT NULL );"; 


private static final String DROP_TABLE_USERS="DROP TABLE IF EXISTS "+TABLE_USERS; 

public MySQLite(Context context) { 
super(context, DATABASE_NAME, null, DATABASE_VERSION); 
Message.mess(context,"CONSTRUCTOR WAS CALLED "); 

} 

@Override 
public void onCreate(SQLiteDatabase db) { 

try { 
db.execSQL(TABLE_CREATE_USERS); 
Message.mess(context,"USERS TABLE WAS CREATED"); 

} catch (SQLException e) { 
Message.mess(context,"ERROR CREATING TABLE "+e);
e.printStackTrace(); //Here i get the error

} 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

db.execSQL(DROP_TABLE_USERS); 
Message.mess(context,"UPGRADE WAS CALLED"); 
onCreate(db); 
} 
}//end of inner class 
} 

And the main activity, where i want to insert data into the DB:

    public class SignUp extends Activity implements View.OnClickListener {

    EditText username,pass,name,mail;
    Button signButton;
    MySQLiteAdapter UserBase;




    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);


        username = (EditText) findViewById(R.id.et_usrn);
        pass = (EditText) findViewById(R.id.et_pass);
        name = (EditText) findViewById(R.id.et_name);
        mail = (EditText) findViewById(R.id.et_mail);

        signButton = (Button) findViewById(R.id.signup_button);

        UserBase=new MySQLiteAdapter(this);

        signButton.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_sign_up, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {

                String u=username.getText().toString();
                String p=pass.getText().toString();
                String n=pass.getText().toString();
                String m=mail.getText().toString();

                long id=UserBase.insertUser(u, p, n, m);

                if(id<0){
                    Message.mess(this,"ERROR INSERTING DATA"+id);

                }else {
                    Message.mess(this, "Succesful sign up");
                }

        }

}

And here is the logcat :

07-12 16:57:50.268    4896-4896/com.example.name.appname E/SQLiteLog﹕ (1) no such table: users_table
07-12 16:57:50.268    4896-4896/com.example.name.appname E/SQLiteDatabase﹕ Error inserting EMAIL=mail@thing USERNAME=user NAME=name PASSWORD=pass
    android.database.sqlite.SQLiteException: no such table: users_table (code 1): , while compiling: INSERT INTO users_table(EMAIL,USERNAME,NAME,PASSWORD) VALUES (?,?,?,?)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:892)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:503)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:726)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1568)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1440)
            at 

com.example.name.appname.MySQLiteAdapter.insertUser(MySQLiteAdapter.java:68)
-->*
                at com.example.name.appname.SignUp.onClick(SignUp.java:72)
-->**
                at android.view.View.performClick(View.java:4442)
                at android.view.View$PerformClick.run(View.java:18473)
                at android.os.Handler.handleCallback(Handler.java:733)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5103)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
                at dalvik.system.NativeStart.main(Native Method)

-->* the line is

long id=db.insert(MySQLite.TABLE_USERS,null,cv);

-->**the line is

 long id=UserBase.insertUser(u, p, n, m);

the Message class contanins just 1 method, which creates a Toast, and prints a message in the Toast. When I run the app, i get the"CONSTRUCTOR WAS CALLED" message, and after that the "ERROR CREATING TABLE" and the "ERROR INSERTING DATA -1" messages. Sorry if the post is a duplicate :)

Aucun commentaire:

Enregistrer un commentaire