android.database.sqlite.SQLiteException:没有这样的表:users_table(代码1):,同时编译:INSERT INTO users_table

时间:2015-07-12 14:42:53

标签: android sqlite

所以我看到并阅读了所有相同的问题,试图解决问题,但我没有找到正确的答案。 我尝试修改VERSION NUMBER,运行应用程序,但我发现了错误。之后我从手机中删除了应用程序,将VERSION NUMBER更改为1,再次运行应用程序...同样的错误。

这是我的代码:

   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 
} 

主要活动,我想将数据插入数据库:

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

        }

}

这是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)

- &gt; *该行

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

- &gt; **该行

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

Message类仅包含1个方法,该方法创建Toast,并在Toast中打印消息。 当我运行应用程序时,我得到了#34; CONSTRUCTOR被调用&#34;消息,然后&#34; ERROR CREATING TABLE&#34;和&#34; ERROR INSERTING DATA -1&#34;消息。 对不起,如果帖子是重复的:)

2 个答案:

答案 0 :(得分:7)

  1. 更改数据库版本

    2.首先 卸载 您的应用程序在模拟器或手机中并重新安装您的应用程序。 我这样认为你的问题将会得到解决。

答案 1 :(得分:2)

表创建代码中存在错误(缺少空格):

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 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)"; 

您的空格键似乎已损坏......;)