0

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

Community
  • 1
  • 1
ZimZux
  • 29
  • 5
  • 6
    If it's for configuration you should use sharedPreference instead of SQLITE. It's like trying to kill a mosquito with a shootgun XD – An-droid Oct 03 '13 at 13:10
  • i still have about 65 buttons, should i still use sharedpreferences? – ZimZux Oct 03 '13 at 14:12

1 Answers1

1

As Yume117 has said, it's better to use SharedPreferences if you want to have some configuration/setting/preference for user. Android Developer guide to SharedPreferences

However, as we're on SQLite topic, it's also good to learn how to use it.

First, how to correctly insert a row:

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 */
    }
} 

Then, how to retrieve the value

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));

I have to admit that this is still not the best approach for SQLite. Try to read and follow the Android Developer guide to SQLite

Andrew T.
  • 4,701
  • 8
  • 43
  • 62