Java 为什么要使用result.getInt(2)/result.getInt(3)和result.getString(0)
我有点困惑,为什么在RoutineTrieved函数中,我在分配ACTIVITYIMAGE时使用result.getInt(2),在分配插槽时使用result.getInt(3)。。。。在colorchange函数中,我在分配日期时使用result.getInt(0) 我之前假设我指向sqlite数据库中的列。但是现在我很困惑。有人能解释一下这些数字是什么意思吗 RoutineRetrieved函数:Java 为什么要使用result.getInt(2)/result.getInt(3)和result.getString(0),java,sqlite,Java,Sqlite,我有点困惑,为什么在RoutineTrieved函数中,我在分配ACTIVITYIMAGE时使用result.getInt(2),在分配插槽时使用result.getInt(3)。。。。在colorchange函数中,我在分配日期时使用result.getInt(0) 我之前假设我指向sqlite数据库中的列。但是现在我很困惑。有人能解释一下这些数字是什么意思吗 RoutineRetrieved函数: private void routineRetrieved() { Curs
private void routineRetrieved() {
Cursor result = myDb.retrieveRoutine(currentDay);
if (result.getCount() == 0) {
// Do nothing
} else {
while (result.moveToNext()) {
int ActivityImage = result.getInt(2);
int Slot = result.getInt(3);
ImageView emptySlot = (ImageView) findViewById(Slot);
emptySlot.setImageResource(ActivityImage);
}
}
}
private void colourChange() {
Cursor result = myDb.checkColour();
if (result.getCount() == 0) {
// Default colour remains
} else {
while (result.moveToNext()) {
String day = result.getString(0);
findViewById(getResources().getIdentifier(day + "button", "id", getPackageName()))
.setBackgroundColor(getResources().getColor(R.color.colorSuccess));
}
}
}
变色功能:
private void routineRetrieved() {
Cursor result = myDb.retrieveRoutine(currentDay);
if (result.getCount() == 0) {
// Do nothing
} else {
while (result.moveToNext()) {
int ActivityImage = result.getInt(2);
int Slot = result.getInt(3);
ImageView emptySlot = (ImageView) findViewById(Slot);
emptySlot.setImageResource(ActivityImage);
}
}
}
private void colourChange() {
Cursor result = myDb.checkColour();
if (result.getCount() == 0) {
// Default colour remains
} else {
while (result.moveToNext()) {
String day = result.getString(0);
findViewById(getResources().getIdentifier(day + "button", "id", getPackageName()))
.setBackgroundColor(getResources().getColor(R.color.colorSuccess));
}
}
}
Database.java
public class Database extends SQLiteOpenHelper
{
public static final String DATABASE_NAME = "application.db";
public static final int DATABASE_VERSION = 9;
// Table Name
public static final String RoutineTable = "Routines";
// Column Names
public static final String RoutineColumn1 = "DayOfWeek";
public static final String RoutineColumn2 = "Activity";
public static final String RoutineColumn3 = "Slot";
public Database(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE `Routines` (`Routine` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,`DayOfWeek` TEXT NOT NULL,`Activity` INTEGER NOT NULL, `Slot` INTEGER NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
onCreate(db);
}
public Cursor retrieveRoutine(String selectedDay) { WHERE DayOfWeek equals selectedDay and store this as result.
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select * from " + RoutineTable + " WHERE DayOfWeek = '" + selectedDay + "'", null);
return result;
}
public boolean insertRoutine(int activityImage, String selectedDay, int activitySlot) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(RoutineColumn1,selectedDay);
contentValues.put(RoutineColumn2,activityImage);
contentValues.put(RoutineColumn3,activitySlot);
long result = db.insert(RoutineTable, null, contentValues);
if(result == -1)
return false;
else
return true;
}
public Cursor checkColour() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("SELECT DayOfWeek FROM " + RoutineTable + " GROUP BY DayOfWeek", null);
return result;
}
方法
getInt
和getString
中的number参数是要获取的列相对于所做查询的索引,从0开始
您有两个不同的查询。在方法checkColor()中,您有以下查询,其中只有一个字段:
"SELECT DayOfWeek FROM " + RoutineTable + " GROUP BY DayOfWeek"
那么,当你打电话的时候
Cursor result = myDb.retrieveRoutine(currentDay);
String fieldValue = result.getString(0);
fieldValue
将具有查询的第一个字段的值,在本例中为DayOfWeek
在另一个查询中也是如此,即表例程的
SELECT*
。在这种情况下,索引指的是CREATE TABLE
语句中的字段位置。方法getInt
和getString
中的number参数是要获取的列的索引,相对于所做的查询,从0开始
您有两个不同的查询。在方法checkColor()中,您有以下查询,其中只有一个字段:
"SELECT DayOfWeek FROM " + RoutineTable + " GROUP BY DayOfWeek"
那么,当你打电话的时候
Cursor result = myDb.retrieveRoutine(currentDay);
String fieldValue = result.getString(0);
fieldValue
将具有查询的第一个字段的值,在本例中为DayOfWeek
在另一个查询中也是如此,即表例程的
SELECT*
。在这种情况下,索引引用CREATE TABLE
语句中的字段位置。它指向一个,whyString day=result.getString(0)
指向主键例程
我不知道(应用程序可能是在主键上实现的),但是我个人不会使用columnIndex和SELECT*
语句,因为更改数据库就足够了,代码将被破坏。它指向一个,whyString day=result.getString(0)
指向主键例程
我看不出来(应用程序可能是在主键上实现的),但是我个人不会将columnIndex与SELECT*
语句一起使用,因为更改数据库就足够了,代码将被破坏。那么,您是说,对于CheckColor查询,它是0,因为它只返回一个字段?当RetrieveRoutine查询引用Database.java文件顶部的CREATETABLE语句中设置的顺序时——换句话说,DayOfWeek(第一个)、Activity(第二个)和Slot(第三个)。几乎!实际上,第一个字段是Routine
,第二个字段是DayOfWeek
,等等(:那么你是说checkcolor查询是0,因为它只返回一个字段?而RetrieveRoutine查询是指Database.java文件顶部的CREATE TABLE语句中设置的顺序,换句话说,DayOfWeek(第一个)、活动(第二个)和插槽(第三个)。差不多!实际上第一个字段是例程
,第二个字段是星期几
,等等(: