简体   繁体   English

我正在为Android进行登录和注册,但是我使用的sqlite数据库在onCreate上崩溃了

[英]I am making a login and sign up page for android but the sqlite database i use crashes on onCreate

package company.mystorage.com.mystorage;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseHelper extends SQLiteOpenHelper{

private static final String DATABASE_NAME = "mystorage.db";
private static final String TABLE_NAME = "userlist";

public static final String COLUMN_1 ="Id";
private static final String COLUMN_2 = "username";
private static final String COLUMN_3 = "password";
SQLiteDatabase db;

    public DatabaseHelper(Context context) {
        // database is created when constructor is called.
        super(context, DATABASE_NAME, null, 1);
        // to invoke onCreate() and onUpgrade() methods to create the tables
        SQLiteDatabase db = this.getWritableDatabase();

    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("create table " + TABLE_NAME + "("
                + COLUMN_1 +" INTEGER PRIMARY KEY AUTOINCREMENT, "
                + COLUMN_2 +" TEXT, "
                + COLUMN_3 +" TEXT)"
        );

    }

    public boolean insertData(String username, String password){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COLUMN_2, username);
        contentValues.put(COLUMN_3, password);
        long result = db.insert(TABLE_NAME, null, contentValues);
        if (result == -1)
            return false;
        else
            return true;
    }

    public boolean searchPass(String user){
        db = this.getReadableDatabase();
        String query = "select * from  " + TABLE_NAME + " where " + COLUMN_2 + " = " + "'" + user + "'";
        Cursor cursor = db.rawQuery(query, null);

        cursor.moveToFirst();
        if (cursor.getCount() > 0) {

            return true;
        }
        cursor.close();
        db.close();

        return false;

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
        onCreate(db);
    }

}

This code will create a database and also contains the functions to search and add users to the database. 此代码将创建一个数据库,并且还包含搜索用户并将用户添加到数据库的功能。 I tried to debug the code and it showed that it crashed when the onCreate was called. 我尝试调试代码,结果表明调用onCreate时崩溃。 I checked the onCreate but the query is being execute correctly. 我检查了onCreate,但查询正确执行。

Edited* I changed the this. 编辑*我改变了这个。 for the db but it still crashed. 为数据库,但它仍然崩溃。

 public void onCreate(SQLiteDatabase db){ this.db.execSQL(...) 

You should be calling execSQL() on the db parameter and not this.db field that has not yet been initialized. 您应该在db参数上调用execSQL() ,而不是尚未初始化的this.db字段。

This should help : 这应该有帮助:

 public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }

Please remove "this". 请删除“此”。

Also, in your method SearchPass(), if cursor has more count than it wont close the cursor and db object. 同样,在您的方法SearchPass()中,如果游标的计数大于其数量,则不会关闭游标和db对象。 It would be a better idea to handle that in Finally block. 最好在Final块中处理它。

在您的onCreate方法中,请勿将thisdb.execSQL(..)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使用 firebase 创建登录页面和注册页面。 我想使用 firebase 实时数据库作为商店用户名和 email - I am trying to make an login page and sign up page with firebase . I want to use firebase realtime database for store username and email 运行getAllComments()方法时,android SQLITE数据库应用程序oncreate崩溃 - android SQLITE database app crashes oncreate when it runs the getAllComments() method 如何在Android中加快SQLite数据库的插入速度? - How can I speed up SQLite database insertions in Android? Android - 为什么我在SQLiteOpenHelper onCreate中得到NullPointerException? - Android - Why am I getting NullPointerException in SQLiteOpenHelper onCreate? 在Android的onCreate()中从SQLite数据库填充ListView - Populate ListView From SQLite database in onCreate() in Android 在 Android SQLite 数据库 onCreate() 中重新抛出异常 - Rethrow Exception in Android SQLite Database onCreate() 如何在SQLite数据库的onCreate函数中使用getWritableDatabase()? - How to use getWritableDatabase() in onCreate function of SQLite database? 在Android中制作计算器。 该应用程序显示在模拟器上没有错误,但是当我按下“计算”按钮时,它崩溃了 - Making a calculator in android. The app shows up on the emulator with no errors but when I press the calculate button it crashes 尝试使用httpPost登录到moodle,但是结果我得到了我尝试登录的页面。 (Android) - Trying to login to moodle with httpPost but as result i get the same page that i am trying to login. (Android) 如何使用 firebase android studio 的电话号码创建登录和注册? - How can I create login and sign up using phone number with firebase android studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM