1

I have developed an Android database application.
The problem is that when I click the add button then a message shows that data was successfully added.
But the data is not inserted into the database.
I then open the database with SQLite viewer and there I can't find any of the data I inserted...

Register page code :

package com.example.dbapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Register extends Activity {

Button add;
TextView b,c;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);
    add = (Button) findViewById(R.id.btnReg);
    b   = (TextView) findViewById(R.id.etName);
    c   = (TextView) findViewById(R.id.etOccu);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
                String name   = b.getText().toString();
                String occ    = c.getText().toString();
                DataCon entry = new DataCon(Register.this);
                entry.open();
                entry.createEntry(name, occ);
                entry.close();
                Toast toast   = Toast.makeText(getApplicationContext(), "Sucess", Toast.LENGTH_SHORT);
                toast.show();
            }
            catch(Exception e) {
                String err  = e.toString();
                Toast toast = Toast.makeText(getApplicationContext(), err,           Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    });
}

Database Connection code :

package com.example.dbapp;

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

public class DataCon {
public static final String rowid = "_id";
public static final String name  = "persons_name";
public static final String hot   = "persons_hotness";

private static final String DB_NAME  = "myDB";
private static final String DB_TABLE = "tbl_me";
private static final int DB_VER      = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DB_NAME, null, DB_VER);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DB_TABLE + " (" + rowid + 
                " INTEGER PRIMARY KEY AUTOINCREMENT, " + name + "TEXT              NOT    NULL, " +
                hot + " TEXT NOT NULL);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)  {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " +DB_TABLE);
        onCreate(db);
    }

}

public DataCon(Context c) {
    ourContext = c;
}

public DataCon open() {
    ourHelper   = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}
public void close() {
    ourHelper.close();
}

public long createEntry(String name2, String occ) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(name, name2);
    cv.put(hot, occ);
    return ourDatabase.insert(DB_TABLE, null, cv);
}
}

Now how can I solve the problem? thanks

3
  • Is there any error in LogCat? Try reading the same data from your app. Commented Feb 6, 2014 at 10:53
  • Post your logcat If any error? Commented Feb 6, 2014 at 11:00
  • No there is no LogCat error. I also trying to read data from my app but failed...:( Commented Feb 6, 2014 at 11:01

1 Answer 1

2

An error in your sql. You haven't provide space between column name and type. Correct one should be

db.execSQL("CREATE TABLE " + DB_TABLE + " (" + rowid + 
" INTEGER PRIMARY KEY AUTOINCREMENT, " + name + " TEXT NOT NULL, " +
                      --------------------------- ^ 
hot + " TEXT NOT NULL);");`
Sign up to request clarification or add additional context in comments.

6 Comments

Do you received any errors? have you seen the created table? Delete your db and try
No there is no error. i have seen the created table using sqlite viewer...but there is no data
Delete the table and run
yes its work...but what is the problem ? I delete the database & run the app & data inserted.....many many thnks....
You have changed your code after creation of db. So after your db created, your new code contains invalid sql. Thats why it was not working
|

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.