0

Im able to write data to a the database in the main activity but im having problems reading and displaying the data in a different activity. I'm using a SQLHelperDatabase class with a 'addData' method (which works fine according to the debugger) and a showData method (and this is where the problem lies). After entering data in the main activity i want to be able to press the send message button and the data to be displayed on the new activity. Ive been stuck on this for a week now and i been searching the WWW and every tutorial says that my code should work. Please can someone help me get to the bottom of this frustrating ordeal.

package com.doors.waynderful.myapp;

import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class DisplayMessageActivity extends ActionBarActivity {
DatabaseHelper db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        db = new DatabaseHelper(this);
        Cursor cursor = db.showData();
        cursor.moveToFirst();

        String nam = cursor.getString(cursor.getColumnIndex(DatabaseHelper.THE_TABLE_COLUMN_NAME));

        if (!cursor.isClosed()){
            cursor.close();
        }




    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(nam);

   setContentView(textView);
    //setContentView(R.layout.activity_display_message);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_display_message, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
   }
}

Below is the SQLHelperDatabase.

package com.doors.waynderful.myapp;

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


public class DatabaseHelper extends SQLiteOpenHelper
{
public static final String THE_DATABASE = "theDatabase";
public static final String THE_TABLE = "infoTable";
public static final String THE_TABLE_COLUMN_ID = "id";
public static final String THE_TABLE_COLUMN_NAME = "name";
public static final String THE_TABLE_COLUMN_DETAILS = "details";

DatabaseHelper(Context context)
{
    super(context,THE_DATABASE, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " +THE_TABLE+"(id integer primary key, name text,     details text) ");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists " + THE_TABLE + "");

    onCreate(db);
}

public void addData(String nameIn, String detailsIn)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues data = new ContentValues();

    data.put("name", nameIn);
    data.put("details", detailsIn);

    db.insert(THE_TABLE,null,data);

    }

    public Cursor showData()
    {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor;
    cursor = db.rawQuery("select * from "+THE_TABLE+"", null);
    return cursor;

    }
}

Below is the MainActivity

package com.doors.waynderful.myapp;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;



public class MainActivity extends ActionBarActivity {

public final static String EXTRA_MESSAGE = "com.doors.waynderful.myapp.MESSAGE";
DatabaseHelper myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void sendMessage(View view){
    myDb = new DatabaseHelper(this);


    Intent intent = new Intent(this,DisplayMessageActivity.class);

    EditText editText = (EditText) findViewById(R.id.edit_name);
    EditText editText1 = (EditText) findViewById(R.id.edit_details);

    String name = editText.getText().toString();
    String details = editText1.getText().toString();

    myDb.addData(name, details);
    //intent.putExtra(EXTRA_MESSAGE,message);

   startActivity(intent);

}
}

LOGCAT shows

03-31 09:40:31.100 1351-1351/com.doors.waynderful.myapp E/SQLiteLog﹕ (1) no such table: infoTable

2
  • have you ever created any table? Commented Mar 30, 2015 at 1:15
  • code now reflects the tables being added. sorry my bad. Commented Mar 31, 2015 at 8:56

1 Answer 1

1

I'm not sure why you think that you must necessarily have a row in the database with an id = 1

If you want to find a row with a specific name, use a sql statement that selects the row with that name.

If you want to list all rows, then select all rows.

But inserting a row, and then assuming that you know it's id simply isn't going to work.

Also, you should use your (currently commented out) code that uses a layout instead of creating a new text view and setting that as the content view.

Finally, you don't seem to have any code that actually creates the table in the database?

Sign up to request clarification or add additional context in comments.

2 Comments

i did assume that if data was added to a table it would automatically be assigned with the index 1 (bad assumption perhaps). I have recently updated the the code to show the table being created, i mistakenly missed it out when composing the post.
Am I not creating the table in the way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.