【发布时间】:2016-05-20 02:09:37
【问题描述】:
我已经建立了 3 个类,1) tableStudents 2) DB 3) MainActivity
到目前为止,代码成功地创建了一个数据库,并将给定的一组编码测试数据插入到数据库中。但是我不知道如何打印这些信息只是为了表明它确实已经成功输入了。
我在大学计算机上,因此无法访问数据库文件
在不浏览文件的情况下,我尝试使用我为获取数据而编写的方法输出列表,但我不确定如何将其物理输出为文本。
完整代码如下:
tableStudents.java
public class tableStudents {
public String name, gender, password, course, modules;
public int age;
//Constructor
public tableStudents()
{
}
//constructor
public tableStudents(String name, String gender, int age, String password, String course, String modules)
{
this.name = name;
this.password = password;
this.gender = gender;
this.age = age;
this.course = course;
this.modules = modules;
}
public static abstract class tableColumns implements BaseColumns
{
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 Modules = "modules";
public static final String Database = "databasename";
public static final String Table = "tablename";
}
}
DB.java
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 Modules = "modules";
public DB(Context context) {
super(context, tableColumns.Database, null, db_version);
}
@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, " +
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 List<tableStudents> getData() {
List<tableStudents> studentList = new ArrayList<tableStudents>();
// 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 {
tableStudents student = new tableStudents();
student.name = cursor.getString(0);
student.password = cursor.getString(1);
student.gender = cursor.getString(2);
student.age = Integer.parseInt(cursor.getString(3));
student.course = cursor.getString(4);
student.modules = cursor.getString(5);
studentList.add(student);
} while (cursor.moveToNext());
}
// return contact list
return studentList;
}
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(Modules, modules);
Log.d("DB", "Inserted Successfully");
return true;
}
}
MainActivity.java
public class MainActivity extends Activity {
Intent appIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DB db = new DB(this);
db.insertStudent("T", "T", 5, "T", "T", "T");
List<tableStudents> outputList = db.getData();
}
public void goHomepage(View v)
{
Intent intent = new Intent(MainActivity.this, Homepage.class);
startActivity(intent);
}
public void goAccount(View v)
{
Intent intent = new Intent(MainActivity.this, MyAccount.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
【问题讨论】:
-
使用
SELECT检索您的数据并与原始数据进行比较? -
我在 DB.java 中的 getData 方法有那个伙伴
-
那么你缺少的部分是什么?您是否需要向其他人提供输出以证明您的代码有效?如果是这样,只需
Log您的数据。 -
我只是想看看插入的信息并证明它已被插入到数据库中以便我自己识别,谢谢
标签: java android eclipse sqlite