2015-07-12 77 views
-1

所以我看到和阅读所有相同的问题,试图解决这个问题,但我没有找到正确答案。 我试图修改版本号,运行应用程序,但我得到了那个错误。之后,我从手机中删除了应用程序,将版本号更改为1,再次运行应用程序...同样的错误。android.database.sqlite.SQLiteException:没有这样的表:users_table(代码1):,编译时:INSERT INTO users_table

这里是我的代码:

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 protected] 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) 

- > *该行是

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

- > **行是

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

Message类contanins只是1的方法,其中创建一个烤面包,并打印在吐司的消息。 当我运行应用程序时,我得到“CONSTRUCTOR WAS CALLED”消息,之后出现“ERROR CREATING TABLE”和“ERROR INSERTING DATA -1”消息。 很抱歉,如果帖子是重复:)

+0

有**许多**缺失的空格... –

+1

为什么你在'onCreate'中保存错误的秘密? –

+0

备注:sqlite不关心varchars。无论如何它都会以文字形式出现。 – njzk2

回答

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

看来你空格键是打破...;)

7
  1. C焊割数据库版本

    或者

    2.First 卸载您的应用程序在仿真器或手机,并重新安装您的应用。 我以这种方式认为你的问题将得到解决。

相关问题