SQLiteDatabase
データを保存するためにアプリにを実装しようとしました。ただし、に慣れるためにテスト プロジェクトを作成しましたSQliteDatabase
。それはかなり単純ですが、それでも正しく動作しません。問題はコードの後で発生します( 用に構築GoogleAPI 7
)。
SQLiteTestActivity :
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();
}
}
});
}
}
データベースハンドラ:
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}