I have been trying to figure out how to implement a SQLite database in my app for the purpose of making a "configuration" page. I have looked at many pages trying to figure it all out, but cannot seem to make it work.
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
are some of the pages I've been looking at.
I want my app to save a text input from an EditText
and then I have a "Save" Button
at the bottom which will save the text when it's clicked. But I would like to know how since I'm not very unfamiliar with this.
The next problem I run into is when I want to call the inputted text to be a text for a button on another layout.xml
This is my Java file
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());
}
});
}
And the 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);
}
I really hope some guys in here can help me with this problem