Android如何将数据插入数据库辅助类

时间:2016-03-14 13:46:49

标签: java android sqlite insert

我已经建立了一个SQLite数据库,它在创建我需要的表方面运行良好。我一直在尝试做的是插入到数据库,但从另一个类(Signup.java)。我想将文本输入抓取到edittext中并将其插入到数据库中,但仅在单击按钮时才会插入。

请参阅下面的DatabaseHelper类:

package com.example.testerrquin.euro2016fanguide;

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

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "Euro2016.db";
public static final String TABLE_USERS = "Users_table";
public static final String TABLE_CAPACITY = "Capacity_table";
public static final int CapacityOne = 1;

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    SQLiteDatabase db = this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("Create table " + TABLE_USERS +" (UserID INTEGER PRIMARY KEY AUTOINCREMENT, Forename TEXT, Surname TEXT, Email TEXT, Password TEXT, Country TEXT)");
    db.execSQL("Create table " + TABLE_CAPACITY +" (CapacityID INTEGER PRIMARY KEY AUTOINCREMENT, Capacity INTEGER)");

}

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

基本上我需要运行以“db.execSQL”开头的行。我可能需要在某处引用'db'来链接到Databasehelper类但不确定。目前我得到'无法解析符号'db'。

CompleteB.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            String grabFirstname = UsernameET.getText().toString();
            String grabSurname = SurnameET.getText().toString();
            String grabEmail = EmailET.getText().toString();
            String grabPassword = PasswordET.getText().toString();
            String grabCountry = CountryDrop.getSelectedItem().toString();

            db.execSQL("Insert into Users_table (Forename, Surname, Email, Password, Country) values ((" + grabFirstname +"),(" + grabSurname +"),(" + grabEmail +"), (" + grabPassword +"), (" + grabCountry +"))");

            Intent i=new Intent(Signup.this,Login.class);
            startActivity(i);
        }
    });

提前致谢。

3 个答案:

答案 0 :(得分:1)

  1. 在DatabaseHelper中将构造函数声明为“SQLiteDatabase db”实例。
  2. 在DatabaseHelper类中创建一个方法,该方法接受需要并插入db
  3. 的param
  4. 然后在您的活动中创建DatabaseHelper的实例并调用您在步骤2中创建的方法
  5. 请检查其他单身人士模式

答案 1 :(得分:1)

为什么不通过声明所有静态方法来使DatabaseHelper成为单例对象?然后,您可以在活动的任何地方调用它。

答案 2 :(得分:1)

在SQL帮助器类中声明您需要的任何方法:

public void sampleMethod(String[] arguments) {
    ContentValues values = new ContentValues();
    values.put("Column1", arguments[0]);
    values.put("Column2", arguments[1]);
    values.put("Column3", arguments[2]);
    ....
    db.insert(TABLE_NAME, null, values);
}

在您的活动中:

@Override
    public void onClick(View v) {

        String grabFirstname = UsernameET.getText().toString();
        String grabSurname = SurnameET.getText().toString();
        String grabEmail = EmailET.getText().toString();
        String grabPassword = PasswordET.getText().toString();
        String grabCountry = CountryDrop.getSelectedItem().toString();

        DatabaseHelper helper = new DatabaseHelper(context); //replace context with your activity context. "this" would refer to your onClickListener so be careful.

        helper.sampleMethod(new String[]{
           "user",
           "pass",
           "foo",
           "bar",
           ...
        });

      //rest of your code
    }

此外,另一方面,请确保在完成数据库后关闭数据库。我个人会在每个方法中调用getWritableDatabase,然后在方法结束时关闭数据库。类似的东西:

public class DatabaseHelper extends SQLiteOpenHelper {

    Context c;

    public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    c = context;
    }

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("Create table " + TABLE_USERS +" (UserID INTEGER PRIMARY KEY AUTOINCREMENT, Forename TEXT, Surname TEXT, Email TEXT, Password TEXT, Country TEXT)");
db.execSQL("Create table " + TABLE_CAPACITY +" (CapacityID INTEGER PRIMARY KEY AUTOINCREMENT, Capacity INTEGER)");
}

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

public void sampleMethod(String[] arguments) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("Column1", arguments[0]);
    values.put("Column2", arguments[1]);
    values.put("Column3", arguments[2]);
    ....
    db.insert(TABLE_NAME, null, values);
    db.close();
}