我正在Android Studio中学习SQLite,并且在从光标到TextView控件输出数据时遇到了一些问题。基本上,我有一个具有Insert按钮的应用程序,静态插入6条记录。单击“显示当前记录”按钮时,我希望根据SQLite数据库中3列的内容更改三个TextView的文本。但是,我似乎只用我当前的代码得到了三个结果,并且Pet Type没有填充。
MainActivity
package com.example.devonryder.week9;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.*;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Helper mHelper;
Cursor cursor;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHelper = new Helper(this);
// Gets the data repository in write mode
db = mHelper.getWritableDatabase();
cursor = UpdateCursor();
}
public void Insert(View view)
{
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
// Insert values
values.put(Contract.dbEntry.COLUMN_OWNER, "Devon");
values.put(Contract.dbEntry.COLUMN_PET_NAME, "woofy");
values.put(Contract.dbEntry.COLUMN_PET_AGE, 11);
values.put(Contract.dbEntry.COLUMN_PET_NAME, "doge");
// Insert the new row, returning the primary key value of the new row
long newRowId = db.insert(Contract.dbEntry.TABLE_NAME, null, values);
// Clear the insert values
values.clear();
// Insert more values
values.put(Contract.dbEntry.COLUMN_OWNER, "Steve");
values.put(Contract.dbEntry.COLUMN_PET_NAME, "fluff");
values.put(Contract.dbEntry.COLUMN_PET_AGE, 12);
values.put(Contract.dbEntry.COLUMN_PET_NAME, "kitty");
newRowId = db.insert(Contract.dbEntry.TABLE_NAME, null, values);
values.clear();
// Insert more values
values.put(Contract.dbEntry.COLUMN_OWNER, "Dan");
values.put(Contract.dbEntry.COLUMN_PET_NAME, "jimmy boy");
values.put(Contract.dbEntry.COLUMN_PET_AGE, 1);
values.put(Contract.dbEntry.COLUMN_PET_NAME, "lizard");
newRowId = db.insert(Contract.dbEntry.TABLE_NAME, null, values);
values.clear();
// Insert more values
values.put(Contract.dbEntry.COLUMN_OWNER, "James");
values.put(Contract.dbEntry.COLUMN_PET_NAME, "axle");
values.put(Contract.dbEntry.COLUMN_PET_AGE, 3);
values.put(Contract.dbEntry.COLUMN_PET_NAME, "Tiger");
newRowId = db.insert(Contract.dbEntry.TABLE_NAME, null, values);
values.clear();
// Insert more values
values.put(Contract.dbEntry.COLUMN_OWNER, "Rick");
values.put(Contract.dbEntry.COLUMN_PET_NAME, "max");
values.put(Contract.dbEntry.COLUMN_PET_AGE, 7);
values.put(Contract.dbEntry.COLUMN_PET_NAME, "dog");
newRowId = db.insert(Contract.dbEntry.TABLE_NAME, null, values);
values.clear();
// Insert more values
values.put(Contract.dbEntry.COLUMN_OWNER, "David");
values.put(Contract.dbEntry.COLUMN_PET_NAME, "slither");
values.put(Contract.dbEntry.COLUMN_PET_AGE, 2);
values.put(Contract.dbEntry.COLUMN_PET_NAME, "snake");
newRowId = db.insert(Contract.dbEntry.TABLE_NAME, null, values);
values.clear();
// Set output
TextView output = (TextView)findViewById(R.id.tvInsertOutput);
output.setText("Just input " + newRowId);
cursor = UpdateCursor();
}
private Cursor UpdateCursor()
{
String columns[] = {"owner", "PetName", "PetType"};
String where = null;
String groupBy = null;
String having = null;
String orderBy = null;
String limit = null;
cursor = db.query(Contract.dbEntry.TABLE_NAME,
null,
where,
null,
groupBy,
having,
orderBy,
limit);
// Return Cursor
return cursor;
}
public void DisplayCurrentRecord(View view)
{
String temp;
final String EOF = "<eof>";
TextView tempOwner = (TextView)findViewById(R.id.tvOwnerOutput);
TextView tempPetName = (TextView)findViewById(R.id.tvPetNameOutput);
TextView tempPetType = (TextView)findViewById(R.id.tvPetTypeOutput);
if (cursor.isBeforeFirst()) { cursor.moveToNext(); }
if (cursor != null)
{
cursor.moveToNext();
// While the cursor is not after the end of the result set
if (cursor.isAfterLast())
{
tempOwner.setText(EOF);
tempPetName.setText(EOF);
tempPetType.setText(EOF);
}
else
{
temp = cursor.getString(cursor.getColumnIndex(Contract.dbEntry.COLUMN_OWNER));
tempOwner.setText(temp);
temp = cursor.getString(cursor.getColumnIndex(Contract.dbEntry.COLUMN_PET_NAME));
tempPetName.setText(temp);
temp = cursor.getString(cursor.getColumnIndex(Contract.dbEntry.COLUMN_PET_TYPE));
tempPetType.setText(temp);
cursor.moveToNext();
}
}
}
public void ResetRecordSet(View view)
{
cursor.moveToFirst();
}
}
合同
package com.example.devonryder.week9;
import android.provider.BaseColumns;
import android.database.sqlite.*;
/**
* Created by Devon Ryder on 3/8/2017.
*/
public final class Contract {
// Stop instantiation via private constructor
private Contract()
{
}
// Table info in the schema
public static class dbEntry implements BaseColumns
{
public static final String TABLE_NAME = "pet";
public static final String COLUMN_OWNER = "owner";
public static final String COLUMN_PET_NAME = "PetName";
public static final String COLUMN_PET_AGE = "PetAge";
public static final String COLUMN_PET_TYPE = "PetType";
}
}
辅助
package com.example.devonryder.week9;
import android.content.Context;
import android.database.sqlite.*;
/**
* Created by Devon Ryder on 3/8/2017.
*/
public class Helper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "pets.db";
// Constructor -- Calls base class constructor
public Helper(Context context) {
super(context, DATABASE_NAME, null , DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + Contract.dbEntry.TABLE_NAME + " (" +
Contract.dbEntry._ID + " INTEGER PRIMARY KEY," +
Contract.dbEntry.COLUMN_OWNER + " TEXT," +
Contract.dbEntry.COLUMN_PET_NAME + " TEXT," +
Contract.dbEntry.COLUMN_PET_AGE + " INT," +
Contract.dbEntry.COLUMN_PET_TYPE + " TEXT)";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + Contract.dbEntry.TABLE_NAME;
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.devonryder.week9.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<Button
android:text="Insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="94dp"
android:id="@+id/btnInsert"
android:onClick="Insert" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="46dp"
android:id="@+id/tvInsertOutput" />
<TextView
android:text="Owner name "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/btnInsert"
android:layout_alignEnd="@+id/btnInsert"
android:layout_marginBottom="85dp"
android:id="@+id/tvOwner" />
<TextView
android:text="Pet Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/tvOwner"
android:layout_alignLeft="@+id/tvOwner"
android:layout_alignStart="@+id/tvOwner"
android:layout_marginTop="25dp"
android:id="@+id/tvPetName" />
<TextView
android:text="Pet Type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="17dp"
android:id="@+id/textView5"
android:layout_below="@+id/tvPetName"
android:layout_alignLeft="@+id/tvPetName"
android:layout_alignStart="@+id/tvPetName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/tvOwner"
android:layout_toRightOf="@+id/tvOwner"
android:layout_toEndOf="@+id/tvOwner"
android:layout_marginLeft="34dp"
android:layout_marginStart="34dp"
android:id="@+id/tvOwnerOutput" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tvPetName"
android:layout_alignRight="@+id/tvOwnerOutput"
android:layout_alignEnd="@+id/tvOwnerOutput"
android:id="@+id/tvPetNameOutput" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView5"
android:layout_toRightOf="@+id/tvOwner"
android:layout_toEndOf="@+id/tvOwner"
android:id="@+id/tvPetTypeOutput" />
<Button
android:text="Reset Records"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnInsert"
android:layout_alignLeft="@+id/tvOwner"
android:layout_alignStart="@+id/tvOwner"
android:onClick="ResetRecordSet"
android:id="@+id/btnResetRecords" />
<Button
android:text="Display Current Record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnResetRecords"
android:layout_alignLeft="@+id/btnResetRecords"
android:layout_alignStart="@+id/btnResetRecords"
android:onClick="DisplayCurrentRecord"
android:id="@+id/btnDisplayCurrentRecord" />
</RelativeLayout>
非常感谢所有帮助。
更新
似乎在我的Insert方法中我实际上没有插入PetType列,因为我将值放到PetName上两次。
此外,我只获得了3(一半)结果,因为我在Insert方法的开头和结尾都移动了Cursor。