Android - 在另一个类中显示数据库信息

时间:2016-02-04 00:08:32

标签: java android mysql database sqlite

我在DB.java之后根据各种教程创建了一个数据库。 他们有使用 SQLite 获取数据并插入数据库的方法。

我的问题是

1)如何让DB.java实例化,因为你看到我只有insertStudent("hello", "male", 4, "password", "computing", "module");的一些测试数据,我想知道我会用它来做什么来插入,我知道我没有在任何地方调用DB.java来做到这一点,但我不确定要采取哪种方式。

2)一旦创建了数据库,我希望它在主要活动上显示数据库中的数据。我决定将 getData 方法设为静态,然后我使用DB.getData(db);在我的主要活动中调用它,但它声明db cannot be resolved to a variable。所以我搞砸了。

感谢任何指导。

以下是我的 DB.java

package com.example.project;

import com.example.project.tableStudents.tableColumns;

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

public class DB extends SQLiteOpenHelper {
    public static final int db_version = 1;  
    public static final String Table = "Students";  
    public static final String Student_ID = "Student_ID";
    public static final String Student_Name = "Student_Name";
    public static final String Student_Password = "Student_Password";
    public static final String Student_Gender = "gender";
    public static final String Student_Age = "age";
    public static final String Student_Course = "course";
    public static final String Student_Modules = "modules";
    public DB(Context context) {
        super(context, tableColumns.Database, null, db_version);
        // Insert Students
        insertStudent("hello", "male", 4, "password", "computing", "module");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //Create Table
        db.execSQL("CREATE TABLE " + Table + "(" + 
                Student_ID + " INTEGER PRIMARY KEY, " +
                Student_Name + " TEXT, " +
                Student_Password + " TEXT, " +
                Student_Gender + " TEXT, " +
                Student_Age + " INTEGER, " +
                Student_Course + " TEXT, " +
                Student_Modules + "TEXT)"
            );
        Log.d("DB", "DB Created");
    }

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

    public static Cursor getData(DB db)
    {
        SQLiteDatabase SQ = db.getReadableDatabase();
        String[] columns ={tableColumns.Student_ID, tableColumns.Student_Password};
        Cursor cursor = SQ.query(tableColumns.Table, columns, null, null, null, null, null);
        return cursor;
    }

    public boolean insertStudent(String name, String gender, int age, String password, String course, String modules) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(Student_Name, name);
        contentValues.put(Student_Password, password);
        contentValues.put(Student_Gender, gender);
        contentValues.put(Student_Age, age);
        contentValues.put(Student_Course, course);
        contentValues.put(Student_Modules, modules);    
        db.insert(Table, null, contentValues);
        //Log for admin insert
        //Log.d("DB", "Inserted Successfully");
        return true;
    }

}

我的 tableStudents.java 用于控制学生表

package com.example.project;
import android.provider.BaseColumns;
public class tableStudents {

    //Constructor
    public tableStudents()
    {

    }

    public static abstract class tableColumns implements BaseColumns
    {
        public static final String Student_ID= "Student_ID";
        public static final String Student_Password = "Student_Password";
        public static final String Student_Gender = "gender";
        public static final String Student_Age = "age";
        public static final String Student_Course = "course";
        public static final String Student_Modules = "modules";
        public static final String Database = "databasename";
        public static final String Table = "tablename";
    }

}

1 个答案:

答案 0 :(得分:0)

1)实例化和插入数据 -

在您的主要活动中

DB db = new DB(this);
db.insertStudent(name, gender, age, password, course, modules);

2)为方便起见,请考虑

Student.java

public class Student{
    public String name, gender, password, course, modules;
    public int age;

Student(){ //constructor
    }

Student(String name, String gender, int age, String password, String course, String modules){ //constructor
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.password = password;
        this.course = course;
        this.modules = modules;
    }
}

现在将DB.java中的getData函数修改为 -

    public List<Student> getData() {
    List<Student> studentList = new ArrayList<Student>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + Table;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Student student = new Student();
            student.name = cursor.getString(0);
            student.gender = cursor.getString(1);
            student.age = Integer.parseInt(cursor.getString(2));
            student.password = cursor.getString(3);
            student.course = cursor.getString(4);
            student.modules = cursor.getString(5);
            studentList.add(student);
        } while (cursor.moveToNext());
    }

    // return contact list
    return studentList;
}