我一直试图弄清楚如何在我的应用程序中实现SQLite数据库,以便创建一个“配置”页面。 我看过许多试图弄清楚它的页面,但似乎无法使它发挥作用。
Creating local DB in android app
https://thenewcircle.com/s/post/55/sql_demo
http://developer.android.com/guide/topics/data/data-storage.html#db
Create SQLite database in android
是我一直在关注的一些页面。
我希望我的应用程序保存来自EditText
的文字输入,然后我在底部有一个“保存”Button
,这会在点击时保存文字。但我想知道,因为我对此并不十分陌生。
我遇到的下一个问题是当我想将输入的文本调用为另一个layout.xml上的按钮的文本时
这是我的Java文件
package com.zimzux.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.ContentValues;
import android.database.sqlite.*;
public class tutorialOne extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.indstil);
DictionaryOpenHelper myDictionary;
myDictionary = new DictionaryOpenHelper(this);
final SQLiteDatabase db = myDictionary.getWritableDatabase();
final EditText indstiltxt1 = (EditText) findViewById(R.id.indstiltxt1);
Button gem1 = (Button) findViewById(R.id.gem1);
gem1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
ContentValues values = new ContentValues();
values.put("id","txt");
db.insert(indstil, null, indstiltxt1.getText());
}
});
}
和DictionaryOpenHelper
package com.zimzux.test;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
/** Helper to the database, manages versions and creation */
public class DictionaryOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "indstil.db";
private static final int DATABASE_VERSION = 1;
// Table name
public static final String TABLE = "indstil";
// Columns
public static final String ID = "id";
public static final String TXT = "txt";
public DictionaryOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + TABLE + "( " + BaseColumns._ID
+ " integer primary key autoincrement, " + ID + " integer, "
+ TXT + " text not null);";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion >= newVersion)
return;
String sql = null;
if (oldVersion == 1)
sql = "alter table " + TABLE + " add note text;";
if (oldVersion == 2)
sql = "";
if (sql != null)
db.execSQL(sql);
}
我真的希望这里的一些人可以帮我解决这个问题
答案 0 :(得分:1)
正如Yume117所说,如果你想为用户提供一些配置/设置/偏好,最好使用SharedPreferences
。 Android Developer guide to SharedPreferences
但是,正如我们在SQLite主题上所做的那样,学习如何使用它也很好。
首先,如何正确插入一行:
gem1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
ContentValues values = new ContentValues();
values.put(DictionaryOpenHelper.ID,"text1"); /* the pair is (column, value) */
values.put(DictionaryOpenHelper.TXT,indstiltxt1.getText().toString()); /* for column name, you can refer using the String constant, known as the contract to the DB */
db.insert(DictionaryOpenHelper.TABLE, null, values); /* insert the values to the corresponding table */
}
}
然后,如何检索值
final SQLiteDatabase db = myDictionary.getReadableDatabase();
String[] projection = {DictionaryOpenHelper.ID, DictionaryOpenHelper.TXT};
String selection = DictionaryOpenHelper.ID+"=?";
String[] selectionArgs = {"text1"};
Cursor cursor = db.query(DictionaryOpenHelper.TABLE, projection, selection, selectionArgs, null, null, null};
if(cursor.moveToFirst()) /* check whether it's exist */
String text = cursor.getString(cursor.getColumnIndex(DictionaryOpenHelper.ID));
我必须承认,这仍然不是SQLite的最佳方法。尝试阅读并遵循Android Developer guide to SQLite