所以我看到和閱讀所有相同的問題,試圖解決這個問題,但我沒有找到正確答案。 我試圖修改版本號,運行應用程序,但我得到了那個錯誤。之後,我從手機中刪除了應用程序,將版本號更改爲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」消息。 很抱歉,如果帖子是重複:)
有**許多**缺失的空格... –
爲什麼你在'onCreate'中保存錯誤的祕密? –
備註:sqlite不關心varchars。無論如何它都會以文字形式出現。 – njzk2