我尝试在我的应用中实施SQLiteDatabase
来存储数据。但是,我编写了一个测试项目来熟悉SQliteDatabase
。它相当简单,但仍然无法正常工作。问题出现在代码(为GoogleAPI 7
构建)之后。
SQLiteTestActicity:
package test.sqlite.db;
import test.sqlite.db.lib.DatabaseHandler;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SQLiteTestActivity extends Activity {
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_SURENAME = "surename";
private static final String KEY_ADDRESS = "address";
private static final String KEY_PHONE = "phone";
private static final String KEY_UID = "uid";
private static final String KEY_TIME = "time";
private static final String KEY_COUNT = "count";
private static final String KEY_DATE = "date";
private static final String ORDERS_TABLE = "orders";
EditText inputName;
EditText inputSurename;
EditText inputAddress;
EditText inputUID;
Button addUserBTN;
Button getUserDetailsBTN;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final DatabaseHandler db = new DatabaseHandler(getBaseContext());
inputName = (EditText) findViewById(R.id.inputName);
inputSurename = (EditText) findViewById(R.id.inputSurename);
inputAddress = (EditText) findViewById(R.id.inputAddress);
inputUID = (EditText) findViewById(R.id.inputUID);
addUserBTN = (Button) findViewById(R.id.addUserBTN);
addUserBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String name = inputName.getText().toString();
String surename = inputSurename.getText().toString();
String address = inputAddress.getText().toString();
String uid = inputUID.getText().toString();
db.addUser(name, surename, address, uid);
try {
Toast.makeText(getApplicationContext(), "Succes!",
Toast.LENGTH_LONG).show();
System.out.printf(name + "\n" + surename + "\n"
+ address + "\n" + uid + "\n");
} catch (NullPointerException e) {
e.printStackTrace();
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});
getUserDetailsBTN = (Button) findViewById(R.id.getUserDetailsBTN);
getUserDetailsBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
System.out.println(db.getUserDetails().toString());
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});
}
}
DatabaseHandler:
package test.sqlite.db.lib;
import java.util.HashMap;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String USER_TABLE = "userT";
static String DATABASE_NAME = "TAXIDB";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_SURENAME = "surename";
private static final String KEY_ADDRESS = "address";
private static final String KEY_PHONE = "phone";
private static final String KEY_UID = "uid";
private static final String KEY_TIME = "time";
private static final String KEY_COUNT = "count";
private static final String KEY_DATE = "date";
private static final String ORDERS_TABLE = "orders";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_USER_TABLE = "CREATE TABLE IF NOT EXISTS " + USER_TABLE
+ "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_SURENAME + " TEXT," + KEY_ADDRESS + " TEXT," + KEY_UID
+ " TEXT" + ")";
db.execSQL(CREATE_USER_TABLE);
// String CREATE_ORDERS_TABLE = "CREATE TABLE IF NOT EXISTS "
// + ORDERS_TABLE + "(" + KEY_ID
// + " INTEGER PRIMARY KEY AUTOINCREMENT," + ")";
// db.execSQL(CREATE_ORDERS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void addUser(String name, String surename, String address, String uid) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name); // Name
values.put(KEY_SURENAME, surename); // Surename
values.put(KEY_ADDRESS, address);
values.put(KEY_UID, uid);
// Inserting Row
db.insert(USER_TABLE, null, values);
db.close(); // Closing database connection
}
public HashMap<String, String> getUserDetails(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + USER_TABLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if(cursor.getCount() > 0){
user.put("name", cursor.getString(1));
user.put("surename", cursor.getString(2));
user.put("address", cursor.getString(3));
user.put("uid", cursor.getString(4));
}
cursor.close();
db.close();
// return user
return user;
}
}
问题:
每当我按下getUserDetailsBTN
时,我都会收到以下内容:
logcat的:
{uid=android.widget.EditText@40523fa8, address=android.widget.EditText@40523418, surename=android.widget.EditText@40522888, name=android.widget.EditText@40520548}
答案 0 :(得分:1)
替换此部分中的代码
@Override
public void onClick(View v) {
try {
System.out.println(db.getUserDetails().toString());
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});`
BY此代码
@Override
public void onClick(View v) {
try {
HashMap<String,String>user = db.getUserDetails();
//then loop on the inputs of the cursor inside the hashmap and print them successfully
System.out.println(db.getUserDetails().toString());
} catch (NullPointerException e) {
e.printStackTrace();
}
}
});