0

I got this error:

no such table: form

I am trying to add three fields which are JSON, JSON_modified and AdminId:

static final String DATABASE_NAME = "db1";
static final int DATABASE_VERSION = 2;
public static final int NAME_COLUMN = 1;

static final String DATABASE_TABLE = "create table " + "adminreg" +
        "( " + "AdminRegID" + " integer primary key autoincrement," + "Rest_name text,Contact_person_name text,text,PASSWORD text,Address text); ";
// Variable to hold the database instance

static final String DATABASE_TABLE_REG="CREATE TABLE IF NOT EXISTS " + " form " +
        "(" + "Id" + " integer primary key autoincrement, " + " JSON text, AdminId text, JSON_modified text);";

public SQLiteDatabase db;

private final Context context;

private DataBaseHelper dbHelper;
public  LoginDatabaseAdapter(Context _context)
{
    context = _context;
    dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public  LoginDatabaseAdapter open() throws SQLException
{
    db = dbHelper.getWritableDatabase();
    return this;
}

public void close()
{
    db.close();
}

public  SQLiteDatabase getDatabaseInstance()
{
    return db;
}

public void insertEntry(String json, String json_modified)
{
    Log.e("Data Insert reached", "yes");

    ContentValues newValues = new ContentValues();
    Log.e("JSON STRING to be insert", json);
    newValues.put("JSON", json);
    newValues.put("JSON_modified", json_modified);
    newValues.put("AdminId", "1");
    db.insert("form", null, newValues);
    Toast.makeText(context, "Data Is Successfully Saved", Toast.LENGTH_LONG).show();
    Log.e("context", "Toast");
}

DataBaseHelper.java

public DataBaseHelper(Context context, String  name, SQLiteDatabase.CursorFactory factory, int version)
{
    super(context, name, factory, version);
}

// Called when no database exists in disk and the helper class needs
// to create a new one.
@Override
public void onCreate(SQLiteDatabase _db)
{
    _db.execSQL(LoginDatabaseAdapter.DATABASE_TABLE_REG);
    _db.execSQL(LoginDatabaseAdapter.DATABASE_TABLE);
}

// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
    // Log the version upgrade.
    Log.w("TaskDBAdapter", "Upgrading from version " + _oldVersion + " to " + _newVersion + ", which will destroy all old data");

    // Upgrade the existing database to conform to the new version. Multiple
    // previous versions can be handled by comparing _oldVersion and _newVersion
    // values.
    // The simplest case is to drop the old table and create a new one.
    _db.execSQL("DROP TABLE IF EXISTS " + "TEMPLATE");
    // Create a new one.
    onCreate(_db);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ammy
  • 39
  • 1
  • 7

3 Answers3

2

Just increase the database version or reinstall your app.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vidhi
  • 397
  • 1
  • 5
  • 17
0
public class LoginDataBaseAdapter {

    public final static String DATABASE_NAME="db1";
    public final static int VERSION=1;

    public final static String TABLE_NAME="form";   
    private final static String ID="ID";
    private final static String JSON="JSON";
    private final static String JSON_MODIFIED="JSON_MODIFIED";
    private final static String ADMIN_ID="ADMIN_ID";



    public final static String CREATE_TABLE="CREATE TABLE " +TABLE_NAME+ " ("+ID+ " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"+JSON+" TEXT NOT NULL," +
            ""+JSON_MODIFIED+" TEXT NOT NULL,"+ADMIN_ID+" TEXT NOT NULL)";

    public final static String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME;


     public Context context;
     public SQLiteDatabase sql_db;
     public DataBaseHelper our_db_helper;

     public LoginDataBaseAdapter(Context context)
        {
            this.context=context;
        }

    public void open() {
            our_db_helper=new DataBaseHelper(context);
            sql_db=our_db_helper.getWritableDatabase();

        }

    public void close() {

            sql_db.close();

        }

    public long dataInsert(String json,String modified_json, String admin_id)   
    {
        try{

                ContentValues data=new ContentValues();

                data.put(JSON,json);
                data.put(JSON_MODIFIED, modified_json);
                data.put(ADMIN_ID, admin_id);

                return  sql_db.insert(TABLE_NAME, null, data);
        }

        catch(Exception ee)
        {

        }
        return -1;
    }


    public String getData()
    {
        try{

                String[] columns=new String[]{ID,JSON,JSON_MODIFIED,ADMIN_ID};
                Cursor content=sql_db.query(TABLE_NAME, columns, null, null, null, null, null);

                int index_id=content.getColumnIndex(ID);
                int index_json=content.getColumnIndex(JSON);
                int index_json_modifier=content.getColumnIndex(JSON_MODIFIED);
                int index_admin_id=content.getColumnIndex(ADMIN_ID);

                String data="";

                if(content==null)
                    return data;

                    for(content.moveToFirst();!content.isAfterLast();content.moveToNext())
                    {


                        data=data+ "Id: "+content.getInt(index_id)+" JSON: "+content.getString(index_json)+
                                " Modified JSON: "+content.getString(index_json_modifier)+" Admin_id: "+content.getString(index_admin_id)+"\n";


                    }

                    return data;

        }catch(Exception ee){
            return "";
        }
    }

}


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

public class DataBaseHelper extends SQLiteOpenHelper
    {
         public DataBaseHelper(Context context) {       
            super(context, LoginDataBaseAdapter.DATABASE_NAME, null, LoginDataBaseAdapter.VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(LoginDataBaseAdapter.CREATE_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL(LoginDataBaseAdapter.DROP_TABLE);
            onCreate(db);
        }
    }   

<br>

Now you can use these code from any class...
example:
(1) ToInsertData:

LoginDataBaseAdapter db=new LoginDataBaseAdapter(YOUR_CLASS_NAME.this);
               db.open();
               long response= db.dataInsert("a", "abd", "abcd");
               db.close();
               if(response!=-1)
                   Toast.makeText(YOUR_CLASS_NAME.this, "Data Inserted...", Toast.LENGTH_LONG).show();
               else
                   Toast.makeText(YOUR_CLASS_NAME.this, "Some Error", Toast.LENGTH_LONG).show();


(2) ToFetchData:

 LoginDataBaseAdapter db=new LoginDataBaseAdapter(YOUR_CLASS_NAME.this);
               db.open();
               Toast.makeText(YOUR_CLASS_NAME.this, db.getData().toString(), Toast.LENGTH_LONG).show();
               db.close();
Abdul Rizwan
  • 3,904
  • 32
  • 31
0

You must uninstall the application and then reinstall it. It should work after that. There may be an older version of the database on your device, which does have the (empty) database in place.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AmDroid
  • 131
  • 5