Android - SQLite3 Multiple Columns
I am having trouble getting my code to insert data from two text boxes into two seperate columns.
Input:
Name=1 Rank=2
Current output:
(Row 1) Name=11 Rank=null
Desired output:
(Row 1) Name=1 Rank=2
Any help would be greatly appreciated.
Code on the add page (text boxes + save button):
package org.x.x;
import org.x.x.R;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Add extends Foundation {
private EditText input, input2;
private Button saveButton;
@Override
public void onCreate(final Bundle savedInstanceState) {
Log.d(MyApplication.APP_NAME, "onCreate");
super.onCreate(savedInstanceState);
this.setContentView(R.layout.add);
// get "Application" object for shared state or creating of expensive resources - like DataHelper
// (this is not recreated as often as each Activity)
this.application = (MyApplication) this.getApplication();
// inflate views
this.input = (EditText) this.findViewById(R.id.Beverage_Txt);
this.input2 = (EditText) this.findViewById(R.id.Rank_Txt);
this.saveButton = (Button) this.findViewById(R.id.save_button);
//this.deleteButton = (Button) this.findViewById(R.id.del_button);
//this.output = (TextView) this.findViewById(R.id.out_text);
// save new data to database (when save button is clicked)
this.saveButton.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
new InsertDataTask().execute(Add.this.input.getText().toString(), Add.this.input2.getText().toString());
Add.this.input.setText("");
Add.this.input2.setText("");
}
});
}
@Override
public void onSaveInstanceState(final Bundle b) {
Log.d(MyApplication.APP_NAME, "onSaveInstanceState");
if ((this.input.getText().toString() != null) && (this.input.getText().toString().length() > 0)) {
b.putString(Add.NAME, this.input.getText().toString());
}
if ((this.input2.getText().toString() != null) && (this.input2.getText().toString().length() > 0)) {
b.putString(Add.RANK, this.input2.getText().toString());
}
super.onSaveInstanceState(b);
}
@Override
public void onRestoreInstanceState(final Bundle b) {
super.onRestoreInstanceState(b);
Log.d(MyApplication.APP_NAME, "onRestoreInstanceState");
String name = b.getString(Foundation.NAME);
String rank = b.getString(Foundation.RANK);
if (name != null) {
// use onSaveInstanceState/onRestoreInstance state to manage state when orientation is changed (and whenever restarted)
// put some text in input box, then rotate screen, text should remain
// COMMENT this out, and try again, text won't be there - you need to maintain this state - esp for orientation changes
// (you can rotate the screen in the emulator by pressing 9 on numeric keypad)
this.input.setText(name);
}
if (rank != null) {
// use onSaveInstanceState/onRestoreInstance state to manage state when orientation is changed (and whenever restarted)
// put some text in input box, then rotate screen, text should remain
// COMMENT this out, and try again, text won't be there - you need to maintain this state - esp for orientation changes
// (you can rotate the screen in the emulator by pressing 9 on numeric keypad)
this.input2.setText(rank);
}
}
private class InsertDataTask extends AsyncTask<String, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(Add.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Inserting data...");
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected Void doInBackground(final String... args) {
Add.this.application.getDataHelper().insert(args[0], args[0]);
return null;
}
// can use UI thread here
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
// reset the output view by retrieving the new data
// (note, this is a naive example, in the real world it might make sense
// to have a cache of the data and just append to what is already there, or such
// in order to cut down on expensive database operations)
//new SelectDataTask().execute();
}
}
@Override
public void onBackPressed() {
this.startActivity(new Intent(Add.this, Compare.class));
}
}
Datahelper code:
package org.x.x;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class DataHelper {
private static final String DATABASE_NAME = "example.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "table1";
private Context context;
private SQLiteDatabase db;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into " + TABLE_NAME + "(name, rank) values (?, ?)";
public DataHelper(Context context) {
this.context = context;
OpenHelper openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
this.insertStmt = this.db.compileStatement(INSERT);
}
public SQLiteDatabase getDb() {
return this.db;
}
public long insert(String name, String rank) {
this.insertStmt.bindString(1, name + rank);
return this.insertStmt.executeInsert();
}
public void deleteAll() {
this.db.delete(TABLE_NAME, null, null);
}
public List<String> selectAll() {
List<String> list = new ArrayList<String>();
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name", "rank" }, null, null, null, null, "id desc");
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
list.add(cursor.getString(1));
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return list;
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, name TEXT, rank TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Example", "Upgrading database, this will drop tables and recreate.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
Template code (shared resources):
package org.x.x;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.widget.TextView;
public class Foundation extends Activity {
public static final String NAME = "NAME";
public static final String RANK = "RANK";
public MyApplication application;
public TextView output;
public class SelectDataTask extends AsyncTask<String, Void, String> {
private final ProgressDialog dialog = new ProgressDialog(Foundation.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Selecting data...");
this.dialog.show();
}
// automatically done on worker thread (separate from UI thread)
protected String doInBackground(final String... args) {
List<String> names = Foundation.this.application.getDataHelper().selectAll();
/*List<String> ranks = Foundation.this.application.getDataHelper().selectAll();*/
StringBuilder sb = new StringBuilder();
for (String name : na开发者_JAVA技巧mes) {
sb.append(name + "\n");
}
/*for (String rank : ranks) {
sb.append(rank + "\n");
}*/
return sb.toString();
}
// can use UI thread here
protected void onPostExecute(final String result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
Foundation.this.output.setText(result);
}
}
}
MyApplication code:
package org.x.x;
import android.app.Application;
import android.util.Log;
public class MyApplication extends Application {
public static final String APP_NAME = "x";
private DataHelper dataHelper;
@Override
public void onCreate() {
super.onCreate();
Log.d(APP_NAME, "APPLICATION onCreate");
this.dataHelper = new DataHelper(this);
}
@Override
public void onTerminate() {
Log.d(APP_NAME, "APPLICATION onTerminate");
super.onTerminate();
}
public DataHelper getDataHelper() {
return this.dataHelper;
}
public void setDataHelper(DataHelper dataHelper) {
this.dataHelper = dataHelper;
}
}
You're just calling bindString on the insert to only bind one value. Try the insert() in DataHelper this way:
public long insert(String name, String rank) {
this.insertStmt.bindString(1, name);
this.insertStmt.bindString(2, rank);
return this.insertStmt.executeInsert();
}
I found the problem, changed (on Add page):
protected Void doInBackground(final String... args) {
Add.this.application.getDataHelper().insert(args[0], args[0]);
return null;
}
To:
protected Void doInBackground(final String... args) {
Add.this.application.getDataHelper().insert(args[0], args[1]);
return null;
}
精彩评论