mercredi 3 février 2016

Android - Displaying database information in another class

I have created a database in DB.java following various tutorials. They have methods to get data and insert into the database using SQLite.

My questions are

1) How do I get DB.java to instantiate, as you see i have insertStudent("hello", "male", 4, "password", "computing", "module"); just for a bit of test data, I want to know what I would do with this in order for it to insert, I know I haven't called DB.java anywhere for it to do that, but I am unsure which way to go about it.

2) Once it has created the database, I want it to display the data from the database on the main activity. I decided to make the getData method static, then I called it in my main activity by using DB.getData(db); however it's stating db cannot be resolved to a variable. So i've messed up somewhere.

Any guidance is appreciated.

Below is my 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;
    }

}

And my tableStudents.java for controlling the students table

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";
    }

}

Aucun commentaire:

Enregistrer un commentaire