更改DB后,表中未找到SQLite(Android App)列

发布于 2025-01-31 18:32:00 字数 8223 浏览 1 评论 0原文

我有一个简单的sqlite我的Android应用程序DB,它必须存储详细信息,例如 - 用户名passwordphno,<代码>电子邮件和帐户类型

public class DBHelper extends SQLiteOpenHelper {
    public static final String DBNAME = "Login.db";
    public DBHelper(Context context) {
        super(context, "Login.db", null, 1);
    }


    @Override
    public void onCreate(SQLiteDatabase MyDB) {
        MyDB.execSQL("create Table users(username TEXT primary key, password TEXT, phno TEXT, email TEXT,address TEXT, type TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase MyDB, int i, int i1) {
        MyDB.execSQL("drop Table if exists users");
    }

    public Boolean insertData(String username, String password, String phno, String email, String address, String type){ 
//**Method for inserting date into the Table -> Users**
        SQLiteDatabase MyDB = this.getWritableDatabase();
        ContentValues contentValues= new ContentValues();
        contentValues.put("username", username);
        contentValues.put("password", password);
        contentValues.put("phno", phno);
        contentValues.put("email", email);
        contentValues.put("address", address);
        contentValues.put("type", type);
        long result = MyDB.insert("users", null, contentValues);
        return result != -1;
    }

    public Boolean checkusername(String username) {
        SQLiteDatabase MyDB = this.getWritableDatabase();
        Cursor cursor = MyDB.rawQuery("Select * from users where username = ?", new String[]{username});
        return cursor.getCount() > 0;
    }

    public Boolean checkusernamepassword(String username, String password){
        SQLiteDatabase MyDB = this.getWritableDatabase();
        Cursor cursor = MyDB.rawQuery("Select * from users where username = ? and password = ?", new String[] {username,password});
        return cursor.getCount() > 0;
    }
    public String getData(String username, String target){
        SQLiteDatabase MyDB = this.getWritableDatabase();
        return String.valueOf(MyDB.rawQuery("Select ? from users where username = ?", new String[]{target, username}));
    }
}

当通过注册活动插入数据时,我会发现一个错误,说表中的地址列不存在

igepactivity.class

public class SignupActivity extends AppCompatActivity {
    private Button signup;
    private DBHelper DB;
    private FloatingActionButton backtn;
    private Spinner spinner;
    private String type;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        spinner = findViewById(R.id.typespinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.Type, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        try {
            spinner.setAdapter(adapter);
        }catch(NullPointerException e){

        }
        username = findViewById(R.id.supusername);
        password = findViewById(R.id.suppassword);
        signup = findViewById(R.id.signupbutton);
        phno = findViewById(R.id.supph);
        email = findViewById(R.id.supemail);
        address = findViewById(R.id.supadd);
        DB = new DBHelper(this);
        backtn = findViewById(R.id.backtn);
        backtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        spinner.setOnItemSelectedListener(this);

        signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String user = username.getText().toString();
                String pass = password.getText().toString();
                String dbpno = phno.getText().toString();
                String dbemail = email.getText().toString();
                String dbaddress = address.getText().toString();
                if(!(user.equals("") && (pass.equals("")))){
                     if(!DB.checkusername(user)){
                         if(DB.insertData(user,pass,dbpno,dbemail,dbaddress,type)){ 
\\**Inserting data here
                             Toast.makeText(SignUpActivity.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
                             finish();
                         }else{
                             Toast.makeText(SignUpActivity.this, "Registration Failed ", Toast.LENGTH_SHORT).show();
                         }
                     }else{
                         Toast.makeText(SignUpActivity.this, "User Already Exists", Toast.LENGTH_SHORT).show();
                     }
                    finish();
                }else{
                    Toast.makeText(SignUpActivity.this, "Fields Cannot be Empty", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

以下是

SQLiteLog: (1) table users has no column named address in "INSERT INTO users(address,username,phno,type,email,password) VALUES (?,?,?,?,?,?)"
E/SQLiteDatabase: Error inserting address=b105 username=1432 phno=8310676220 type=Donator [email protected] password=1234
    android.database.sqlite.SQLiteException: table users has no column named address (code 1 SQLITE_ERROR): , while compiling: INSERT INTO users(address,username,phno,type,email,password) VALUES (?,?,?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1047)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:654)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:62)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:34)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1700)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1571)
        at com.example.foodwaste.DBHelper.insertData(DBHelper.java:43)
        at com.example.foodwaste.SignUpActivity$2.onClick(SignUpActivity.java:65)
        at android.view.View.performClick(View.java:7455)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
        at android.view.View.performClickInternal(View.java:7432)
        at android.view.View.access$3700(View.java:835)
        at android.view.View$PerformClick.run(View.java:28810)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

该日志中不存在的日志,但根据上述日志应该:/

table users has no column named address in "INSERT INTO users(address,username,phno,type,email,password) VALUES (?,?,?,?,?,?)"


android.database.sqlite.SQLiteException: table users has no column named address (code 1 SQLITE_ERROR): , while compiling: INSERT INTO users(address,username,phno,type,email,password)

我已经检查了XML中的所有ID,一切似乎都很好。自从我将DB从仅接受用户名和密码更新到上面提到的所有字段以来,此错误已经开始发生。我假设我必须运行onupgrade()方法才能删除旧表并创建一个新表格?我该怎么做。

我是Android和数据库的新手,因此对任何帮助都表示赞赏。

I have a simple SQLite DB for my Android app that has to store details such as - username, password, phno, email and account type.

public class DBHelper extends SQLiteOpenHelper {
    public static final String DBNAME = "Login.db";
    public DBHelper(Context context) {
        super(context, "Login.db", null, 1);
    }


    @Override
    public void onCreate(SQLiteDatabase MyDB) {
        MyDB.execSQL("create Table users(username TEXT primary key, password TEXT, phno TEXT, email TEXT,address TEXT, type TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase MyDB, int i, int i1) {
        MyDB.execSQL("drop Table if exists users");
    }

    public Boolean insertData(String username, String password, String phno, String email, String address, String type){ 
//**Method for inserting date into the Table -> Users**
        SQLiteDatabase MyDB = this.getWritableDatabase();
        ContentValues contentValues= new ContentValues();
        contentValues.put("username", username);
        contentValues.put("password", password);
        contentValues.put("phno", phno);
        contentValues.put("email", email);
        contentValues.put("address", address);
        contentValues.put("type", type);
        long result = MyDB.insert("users", null, contentValues);
        return result != -1;
    }

    public Boolean checkusername(String username) {
        SQLiteDatabase MyDB = this.getWritableDatabase();
        Cursor cursor = MyDB.rawQuery("Select * from users where username = ?", new String[]{username});
        return cursor.getCount() > 0;
    }

    public Boolean checkusernamepassword(String username, String password){
        SQLiteDatabase MyDB = this.getWritableDatabase();
        Cursor cursor = MyDB.rawQuery("Select * from users where username = ? and password = ?", new String[] {username,password});
        return cursor.getCount() > 0;
    }
    public String getData(String username, String target){
        SQLiteDatabase MyDB = this.getWritableDatabase();
        return String.valueOf(MyDB.rawQuery("Select ? from users where username = ?", new String[]{target, username}));
    }
}

When inserting data Through the signup Activity I get an error saying that Address column in the table does not exist

SignupActivity.class

public class SignupActivity extends AppCompatActivity {
    private Button signup;
    private DBHelper DB;
    private FloatingActionButton backtn;
    private Spinner spinner;
    private String type;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        spinner = findViewById(R.id.typespinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.Type, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        try {
            spinner.setAdapter(adapter);
        }catch(NullPointerException e){

        }
        username = findViewById(R.id.supusername);
        password = findViewById(R.id.suppassword);
        signup = findViewById(R.id.signupbutton);
        phno = findViewById(R.id.supph);
        email = findViewById(R.id.supemail);
        address = findViewById(R.id.supadd);
        DB = new DBHelper(this);
        backtn = findViewById(R.id.backtn);
        backtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        spinner.setOnItemSelectedListener(this);

        signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String user = username.getText().toString();
                String pass = password.getText().toString();
                String dbpno = phno.getText().toString();
                String dbemail = email.getText().toString();
                String dbaddress = address.getText().toString();
                if(!(user.equals("") && (pass.equals("")))){
                     if(!DB.checkusername(user)){
                         if(DB.insertData(user,pass,dbpno,dbemail,dbaddress,type)){ 
\\**Inserting data here
                             Toast.makeText(SignUpActivity.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
                             finish();
                         }else{
                             Toast.makeText(SignUpActivity.this, "Registration Failed ", Toast.LENGTH_SHORT).show();
                         }
                     }else{
                         Toast.makeText(SignUpActivity.this, "User Already Exists", Toast.LENGTH_SHORT).show();
                     }
                    finish();
                }else{
                    Toast.makeText(SignUpActivity.this, "Fields Cannot be Empty", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

Here are the logs

SQLiteLog: (1) table users has no column named address in "INSERT INTO users(address,username,phno,type,email,password) VALUES (?,?,?,?,?,?)"
E/SQLiteDatabase: Error inserting address=b105 username=1432 phno=8310676220 type=Donator [email protected] password=1234
    android.database.sqlite.SQLiteException: table users has no column named address (code 1 SQLITE_ERROR): , while compiling: INSERT INTO users(address,username,phno,type,email,password) VALUES (?,?,?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1047)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:654)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:62)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:34)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1700)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1571)
        at com.example.foodwaste.DBHelper.insertData(DBHelper.java:43)
        at com.example.foodwaste.SignUpActivity$2.onClick(SignUpActivity.java:65)
        at android.view.View.performClick(View.java:7455)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
        at android.view.View.performClickInternal(View.java:7432)
        at android.view.View.access$3700(View.java:835)
        at android.view.View$PerformClick.run(View.java:28810)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

Where it says column address does not exist but according to the above log it should :/

table users has no column named address in "INSERT INTO users(address,username,phno,type,email,password) VALUES (?,?,?,?,?,?)"


android.database.sqlite.SQLiteException: table users has no column named address (code 1 SQLITE_ERROR): , while compiling: INSERT INTO users(address,username,phno,type,email,password)

I have checked all the ID's in my xml and everything seems to be fine. This error has started to occur since I updated the DB from only accepting username and password to all the fields mentioned above. I am assuming I have to run the onUpgrade() method to drop the old table and create a new one? How would I go about doing that.

I'm Very new to Android and Databases so any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文