是的,这是家庭作业,但我尽职尽责,但我被困住了!希望有人可以提供帮助!
我的 rawQuery 有问题。它没有显示任何东西。我很确定数据库不是空的,因为我添加了几个插入语句进行测试(我也尝试过put()
and insert()
),但仍然没有任何结果。
问题是,当我运行我的代码时,我没有收到任何错误或异常,但它从未进入 logDatabase ( while !c.moveToNext
) 中的 while 循环。
我做了相当多的研究,但我看到的所有书籍似乎都告诉我要这样做。也许我遗漏了一些明显的东西,但我一生都无法弄清楚它是什么。谢谢!
package jschuler.cs211d.hw06b;
import android.app.Activity;
import android.os.*;
import android.widget.*;
import android.util.Log;
import android.view.*;
import java.util.*;
import java.io.*;
import android.content.*;
import android.database.*;
import android.database.sqlite.SQLiteDatabase;
public class play extends Activity
implements RadioGroup.OnCheckedChangeListener
{
private static final String DBNAME = "statesDB";
static final String STATE = "state", CAPITAL = "capital";
//*****************************onCreate()*****************************
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
// Check to see if SD card is available. If not, warn the user
if(!sdCardAvailable())
{
Toast t = Toast.makeText(getApplicationContext(),
"No SD card available.",
Toast.LENGTH_SHORT);
t.show();
}
else
{
Toast t = Toast.makeText(getApplicationContext(),
"SD card is available.",
Toast.LENGTH_SHORT);
t.show();
}
loadDB();
}
//**************************onCheckChanged()**************************
// Set the variable that represents the type, depending on
// what the user has selected in the radio group.
public void onCheckedChanged(RadioGroup g, int CheckedId)
{
}
//******************************loadDB()******************************
// Create the data structures to hold the data
public void loadDB()
{
SQLiteDatabase db;
db = openOrCreateDatabase("statesDB",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.setVersion(1);
db.setLockingEnabled(true);
db.execSQL("CREATE TABLE if not exists states(_id integer primary key autoincrement, STATE text, CAPITAL text);");
Scanner sc;
try
{
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath());
File f = new File(dir, "US_states");
sc = new Scanner(f);
// Skip the first line
sc.nextLine();
sc.nextLine();
// Read in the names of the states and capitals.
while(sc.hasNextLine())
{
String[] nextItem = sc.nextLine().split("\\s{2,}");
ContentValues cv = new ContentValues();
cv.put(STATE, nextItem[0]);
db.insert("states", STATE, cv);
cv.put(CAPITAL, nextItem[1]);
db.insert("states", CAPITAL, cv);
}
}
catch (FileNotFoundException e)
{
Toast t = Toast.makeText(getApplicationContext(),
"Error loading data.",
Toast.LENGTH_SHORT);
t.show();
}
db.close();
logDatabase();
}
//***************************logDatabase()****************************
public void logDatabase()
{
SQLiteDatabase db;
db = openOrCreateDatabase("statesDB",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.execSQL("INSERT INTO states (STATE) values ('AB');");
db.execSQL("INSERT INTO states (STATE) values ('CD');");
db.execSQL("INSERT INTO states (STATE) values ('EF');");
ContentValues cv = new ContentValues();
cv.put(STATE, "AB");
db.insert("states", STATE, cv);
Cursor c = db.rawQuery("SELECT STATE, CAPITAL FROM states",null);
while(!c.moveToNext())
{
Log.d("mtn","in moveToNext()");
Log.d("state",c.getString(0));
Log.d("capital",c.getString(1));
}
db.close();
}
//*************************sdCardAvailable()**************************
// Determine whether there is an SD card available and if it is
// writable.
public boolean sdCardAvailable()
{
boolean storageAvailable = false;
boolean storageWritable = false;
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state))
{
storageAvailable = storageWritable = true;
}
else if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
{
storageAvailable = true;
storageWritable = false;
}
else
{
storageAvailable = storageWritable = false;
}
return (storageWritable && storageAvailable);
}
}