[Android Q&A, Help & Troubleshooting] mydoople:
Android SQLLite Sample

hello guys im new for xda, and this is a my first thread.please give me your feed back. :)

DataBase Setting Java File

package com.example.mydsqllite;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBCon extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "myDoople";

private static final String TABLE_LABELS = "Customer";

private static final String KEY_ID = "ID";
private static final String KEY_NAME = "CName";

public DBCon(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT)";
db.execSQL(CREATE_CATEGORIES_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);

onCreate(db);
}

/**
* Inserting new lable into lables table
* */
public void insertLabel(String label) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAME, label);

db.insert(TABLE_LABELS, null, values);
db.close();
}

/**
* Getting all labels returns list of labels
* */

public void deleteLabel(String label1) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_NAME, label1);

db.delete(TABLE_LABELS, KEY_NAME + "=?", new String[] {label1});
db.close();

}

public List<String> getAllLabels() {
List<String> labels = new ArrayList<String>();

String selectQuery = "SELECT * FROM " + TABLE_LABELS;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}

cursor.close();
db.close();

return labels;
}
}

Main Activity File

package com.example.mydsqllite;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener,
OnItemSelectedListener {

private Spinner sp;
private Button bt;
private Button dl;
private EditText tx;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

UI();
InitSpinner();

}

public void UI() {

sp = (Spinner) findViewById(R.id.spinner1);
sp.setOnItemSelectedListener(this);

bt = (Button) findViewById(R.id.btUpdate);
bt.setOnClickListener(this);

dl = (Button) findViewById(R.id.btDel);
dl.setOnClickListener(this);

tx = (EditText) findViewById(R.id.TxtCus);

}

private void InitSpinner() {

DBCon db = new DBCon(getApplicationContext());

List<String> lables = db.getAllLabels();

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);

dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

sp.setAdapter(dataAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

String label = arg0.getItemAtPosition(arg2).toString();

Toast.makeText(arg0.getContext(), "Customer Name : " + label,
Toast.LENGTH_LONG).show();

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}

@Override
public void onClick(View v) {
if (v == bt) {
String label = tx.getText().toString();

if (label.trim().length() > 0) {

DBCon db = new DBCon(getApplicationContext());

db.insertLabel(label);

tx.setText("");

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tx.getWindowToken(), 0);

InitSpinner();
}

} else if (v == dl) {
String CName = tx.getText().toString();

if (CName.trim().length() > 0) {

DBCon db = new DBCon(getApplicationContext());

db.deleteLabel(CName);

tx.setText("");

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tx.getWindowToken(), 0);
InitSpinner();
}
} else {
Toast.makeText(getApplicationContext(), "Please Enter Customer Name",
Toast.LENGTH_SHORT).show();

}

}

}