我需要帮助,以便如何从SQLite数据库检索数据并将其放入微调框。 另外,我无法打开我的android应用程序和LogCat说“没有这样的列:名称”。我甚至检查过我的DBAdapter类和非DBAdapter类。从SQLite数据库显示数据到微调框的问题[Android]
我有两个DBAdapter类(InfoDBAdapter.java和BuddiesDBAdapter.java)都链接到我的PersonalInfo.java类。
下面是从数据库检索数据并放入微调器(PersonalInfo.class)的代码。
InfoDBAdapter infoDB = new InfoDBAdapter(this);
//BuddiesDBAdapter buddyDB = new BuddiesDBAdapter(this);
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
BuddiesDBAdapter buddyDB = new BuddiesDBAdapter(this);
buddyDB.open();
Cursor c = buddyDB.getAllContacts();
//Create an array to specify which fields want to display
String[] from = new String[] {"name"};
//Create an array of the display item want to bind our data to
int[] to = new int[] {R.id.fName};
nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
nameSpinner.setAdapter(adapter);
buddyDB.close();
这些是用于将信息插入数据库的插入代码。
btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
infoDB.open();
long id;
Spinner nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
String NameValue = nameSpinner.getSelectedItem().toString();
EditText txtDate = (EditText) findViewById(R.id.txtDate);
String DateValue = txtDate.getText().toString();
EditText txtType = (EditText) findViewById(R.id.txtType);
String TypeValue = txtType.getText().toString();
EditText txtLikes = (EditText) findViewById(R.id.txtLikes);
String LikesValue = txtLikes.getText().toString();
EditText txtDislikes = (EditText) findViewById(R.id.txtDislikes);
String DislikesValue = txtDislikes.getText().toString();
id = infoDB.insertContact(NameValue, DateValue, TypeValue, LikesValue, DislikesValue);
infoDB.close();
Toast.makeText(getBaseContext(),
"Your information is saved successfully!", Toast.LENGTH_SHORT).show();
}
});
下面是两种将对DBAdapter类
InfoDBAdapter.java
BuddiesDBAdapter.java
public class BuddiesDBAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "buddy_name";
private static final String TAG = "BuddiesDBAdapter";
private static final String DATABASE_NAME = "anniversary";
private static final String DATABASE_TABLE = "buddiesList";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE = "create table namesList(_id integer primary key autoincrement, "
+ "buddy_name text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public BuddiesDBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(DATABASE_CREATE);
} catch (SQLException e)
{
e.printStackTrace();
}
}// end onCreate()
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS namesList");
onCreate(db);
}// end onUpgrade()
}//end DatabaseHelper class
public BuddiesDBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}// end open()
public void close()
{
DBHelper.close();
}// end close()
public long insertContact(String buddy_name)
{
ContentValues values = new ContentValues();
values.put(KEY_NAME, buddy_name);
return db.insert(DATABASE_TABLE, null, values);
}//end insertContact()
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}// end deleteContact()
public Cursor getAllContacts()
{
return db.query(DATABASE_TABLE, new String[]
{ KEY_ROWID, KEY_NAME }, null, null, null, null, null);
}// end getAllContacts()
public Cursor getContact(long rowId) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_NAME }, KEY_ROWID + "=" + rowId,
null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}// end getContact()
public boolean updateContact(long rowId, String buddy_name)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, buddy_name);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}//end updateContact()
}
我需要这方面的帮助。任何帮助,将不胜感激。非常感谢!
中,列名'日期'是导致问题,请尝试使用'_date'重命名它 – Lucifer 2012-08-06 03:53:06
当我重新命名为_date时,我仍然有同样的问题。 – Preeyah 2012-08-06 03:59:26
LogCat仍然说:08-06 12:00:12.349:E/AndroidRuntime(536):引起:java.lang.IllegalArgumentException:列'name'不存在 – Preeyah 2012-08-06 04:00:44