gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:31:57 25 4
gpt4 key购买 nike

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

我的问题是

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

2)创建数据库后,我希望它在主 Activity 上显示数据库中的数据。我决定将 getData 方法设为静态,然后使用 DB.getData(db); 在我的主要 Activity 中调用它,但它指出 db 无法解析到一个变量。所以我在某个地方搞砸了。

感谢任何指导。

下面是我的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) 实例化并插入数据 -

在您的主要 Activity 中

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

2)为了方便起见,请考虑

学生.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;
}

关于java - Android - 在另一个类中显示数据库信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35190206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com