androidnoob androidnoob - 9 months ago 37
Android Question

Adding tables in SQLite android

I was researching on how to add tables in an sqlite database for more information but I can't have a grip on how it was truly done.

I have this code:

package com.example.database;

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

public class SQLiteAdapter {

public static final String MYDATABASE_NAME = "SCORING";
public static final String MYDATABASE_TABLE = "SCORING_TABLE";
public static final String MYDATABASE_TABLE1 = "Assessment";
public static final int MYDATABASE_VERSION = 3;
public static final String KEY_ID = "_id";
public static final String KEY_CONTENT1 = "Content1";
public static final String KEY_CONTENT2 = "Content2";
public static final String KEY_CONTENT3 = "Content3";

//create table SCORING (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_CONTENT1 + " text not null, "
+ KEY_CONTENT2 + " text not null, "
+ KEY_CONTENT3 + "text not null);";

private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;

private Context context;

public SQLiteAdapter(Context c){
context = c;

public SQLiteAdapter openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;

public SQLiteAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;

public void close(){

public long insert(String content1, String content2, String content3){

ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT1, content1);
contentValues.put(KEY_CONTENT2, content2);
contentValues.put(KEY_CONTENT3, content3);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);

public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);

public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2, KEY_CONTENT3};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, null);

return cursor;

public class SQLiteHelper extends SQLiteOpenHelper {

public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);

public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// If you need to add a column
if (newVersion > oldVersion) {

and for the second one I'm having a hard time inputting data to the second table

package com.example.database;

import com.example.database.R;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidSQLite extends Activity {

EditText inputContent2;
TextView textView1, textView2;
Button buttonAdd, buttonDeleteAll;

private SQLiteAdapter mySQLiteAdapter;
ListView listContent;

SimpleCursorAdapter cursorAdapter;
Cursor cursor;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

textView1 = (TextView)findViewById(;
textView2 = (TextView)findViewById(;
inputContent2 = (EditText)findViewById(;
buttonAdd = (Button)findViewById(;
buttonDeleteAll = (Button)findViewById(;
listContent = (ListView)findViewById(;

mySQLiteAdapter = new SQLiteAdapter(this);

cursor = mySQLiteAdapter.queueAll();
String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CONTENT1, SQLiteAdapter.KEY_CONTENT2, SQLiteAdapter.KEY_CONTENT3};
int[] to = new int[]{,,,};
cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);



Button.OnClickListener buttonAddOnClickListener
= new Button.OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub
int a=Integer.parseInt(textView1.getText().toString());
int b=a+2;
String s1 = String.valueOf(b);
Toast.makeText(getApplicationContext(), "Wrong",

String data1 = textView1.getText().toString();
String data2 = inputContent2.getText().toString();
String data3 = textView2.getText().toString();
mySQLiteAdapter.insert(data1, data2, data3);



Button.OnClickListener buttonDeleteAllOnClickListener
= new Button.OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub


protected void onDestroy() {
// TODO Auto-generated method stub

private void updateList(){


Answer Source

I personally found using ContentProviders to be a clearer and easier to maintain way to manage multiple tables.

There is a nice article here that explains ContentProviders and other ways of dealing with android database/