1

Im following a tutorial I want to insert a row into table while doing this Its run time fatal exception while inserting a row into database Its Helper database class: DatabasaeAdaptor.java

package com.example.sarahn.inserttable;

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

public class DatabaseAdaptor {

Helperdb helperdb;
DatabaseAdaptor(Context context){
    helperdb = new Helperdb(context);
}

public long insertdata(String name,String password){

    SQLiteDatabase db = helperdb.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(helperdb.NAME,name);
    contentValues.put(helperdb.PASSWORD,password);
    long id = db.insert(helperdb.TABLE_NAME,null,contentValues);
    return id;
}

static class Helperdb extends SQLiteOpenHelper
{
   private static final String DATABASE_NAME = "insert";
   private static final String TABLE_NAME = "table";
   private static final int DATABASE_VERSION = 1;
   private static final String UID = "_name";
   private static final String NAME = "user";
   private static final String PASSWORD = "pw";
   private static final String CREATE_TABLE = "CREATE TABLE  

"+TABLE_NAME+"("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+"  

VARCHAR(255), "+PASSWORD+" VARCHAR(255));";
   private static final String DROP_TABLE = "DROP TABLE IF EXISTS"  

+TABLE_NAME;
   private Context context;


   //context, database name,
   public Helperdb (Context context) {
       super(context,DATABASE_NAME, null, DATABASE_VERSION);
       this.context= context;
       Message.message(context, "constructor called");
   }

   @Override
   public void onCreate(SQLiteDatabase db) {
       try {
           db.execSQL(CREATE_TABLE);
           Message.message(context, "OnCreate called");
       }catch (SQLException e)
       {
           Message.message(context, "" + e);
       }
   }

   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int  

newVersion) {

       try {
           db.execSQL(DROP_TABLE);
           onCreate(db);
           Message.message(context, "Onupgrade called");
       }catch (SQLException e)
       {

           Message.message(context,""+e );
           Message.message(context, "Error");
       }

   }
 }
}

And its MainActivity.java

package com.example.sarahn.inserttable;

import android.app.Activity;
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 Activity {
EditText username;
EditText password;
DatabaseAdaptor db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    username =(EditText) findViewById(R.id.etname);
    password = (EditText) findViewById(R.id.etpass);


}

@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 adduser(View view)
{
    String user = username.getText().toString();
    String pass = password.getText().toString();
    long count =db.insertdata(user,pass);
    if(count<0)
    {
        Message.message(this,"Unsucessful");
    }else
    {
        Message.message(this,"Sucessfull");
    }


}
}

Should it adduser method in mainactivity should be look like this?

public void adduser(View view)
{
    String user = username.getText().toString();
    String pass = password.getText().toString();
    db=new DatabaseAdaptor(getApplicationContext());
    long count =db.insertdata(user,pass);
    if(count<0)
    {
        Message.message(this,"Unsucessful");
    }else
    {
        Message.message(this,"Sucessfull");
    }
Sarah
  • 11
  • 3
  • Where you are initializing Database adapter? That code is not there in your code snippet. In which line you are getting IllegalStateException? – Suhas K Aug 16 '15 at 08:02
  • Yes you should initialize first. Its better to write following line of code in onCreate method. db=new DatabaseAdaptor(getApplicationContext()); – Suhas K Aug 16 '15 at 08:17
  • I worked but there is error in this statement, whats wrong private static final String DROP_TABLE = "DROP TABLE IF EXISTS" +TABLE_NAME; – Sarah Aug 16 '15 at 08:18
  • constructor called an error(while creating table) and end up with unsucessful toast – Sarah Aug 16 '15 at 08:21
  • No need of calling db.execSQL(DROP_TABLE); in onUpgrade method. This will delete the entire table – Suhas K Aug 16 '15 at 08:22
  • It was part of it so included upgrade there, yes you are right there is no need of upgrade. There is problem with creating table – Sarah Aug 16 '15 at 08:24
  • Remove following lines of code from onUpgrade method db.execSQL(DROP_TABLE); onCreate(db); – Suhas K Aug 16 '15 at 08:27
  • now you are getting illegal state exception or syntax error while executing create table.? – Blue_Alien Aug 16 '15 at 08:28
  • It saying: android.database.sqlite.SQLiteException:near "table":syntax error(code 1)while compiling: CREATE TABLE table(_name) INTEGER PRIMARY KEY AUTOINCREMENT, user VARCHAR(255), pw(255)); – Sarah Aug 16 '15 at 08:36

1 Answers1

0

please use a valid table name! it seems like you are using table as table name this will cause error!.

You need to leave a white space before table name! please use the following statement to create your table,

 private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+"("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(255), "+PASSWORD+" VARCHAR(255));";

Note: numeric arguments in parentheses that following the type name (ex: "VARCHAR(255)") are ignored by SQLite - SQLite does not impose any length restrictions

SQLite does not enforce the length of a VARCHAR. You can declare a VARCHAR(10) and SQLite will be happy to let you put 500 characters in it. And it will keep all 500 characters intact - it never truncates.

Blue_Alien
  • 2,148
  • 2
  • 25
  • 29