如何获取某人已输入文本字段的文本并将其输入我的SQLite数据库?

时间:2017-07-04 02:11:50

标签: android sqlite

所以,我正在为我的新应用创建登录活动。如果用户正在注册,我将需要将他们输入的信息存储到注册表单中以存储在我的SQLite数据库中。我该怎么做呢?这就是我到目前为止所做的:

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "USERS";
    private static final int DB_VERSION = 1;

    DatabaseHelper(Context context){
        super(context,DB_NAME,null,DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE DATABASE USERS("
                + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "USERNAME TEXT, "
                + "EMAIL_ADDRESS TEXT, "
                + "PASSWORD TEXT); ");
        insertUser(db, "");


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    private static void insertUser (SQLiteDatabase db, String username, String email, String password){
        ContentValues userValues = new ContentValues();
        userValues.put("USERNAME", username);
        userValues.put("EMAIL_ADDRESS", email);
        userValues.put("PASSWORD", password);
        db.insert("USERS", null, userValues);
    };
} 

1 个答案:

答案 0 :(得分:1)

您已经进行查询以从SQLite数据库中获取记录。

以下是获取所有用户的简单示例:

public List<User> getAllUsers() {
    SQLiteDatabase db = this.getReadableDatabase();

    List<User> users = new ArrayList<>();

    Cursor cursor = db.query("USERS", null, null, null, null, null, null);
    if (cursor.moveToFirst()) {
        do {
            User user = new User();
            user.setUsername(cursor.getString(1));
            user.setEmail(cursor.getString(2));
            users.add(user);
        } while (cursor.moveToNext());
    }

    return users;
}

您可以在以下链接中阅读更多相关信息:

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/