-2

06-22 09:28:25.456 26616-26616/com.example.dell.regform E/SQLiteDatabase: Error inserting message=dfdsfsdf phone=123 name=Name android.database.sqlite.SQLiteException: table reg has no column named message (code 1): , while compiling: INSERT INTO reg(message,phone,name) VALUES (?,?,?) at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467) at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339) at com.example.dell.regform.myDbHandler.addUser(myDbHandler.java:52) at com.example.dell.regform.MainActivity.addDetail(MainActivity.java:32) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:4204) at android.view.View$PerformClick.run(View.java:17355) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method)

package com.example.dell.regform;

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

/**
 * Created by dell on 17-Jun-16.
 */
public class myDbHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "register.db";
    private static final String TABLE_REG = "reg";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_PHONE = "phone";
    private static final String COLUMN_MESSAGE = "message";

   public myDbHandler(Context context) {
       super(context, DATABASE_NAME,null,DATABASE_VERSION);
   }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE "+ TABLE_REG +"("
                + COLUMN_ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"+
                COLUMN_NAME + " TEXT," +
                COLUMN_PHONE + " TEXT," +
                COLUMN_MESSAGE + " TEXT" +
                ")";
        db.execSQL(query);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_REG);

        onCreate(db);

    }

    public void addUser(Registration reg) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, reg.getName());
        values.put(COLUMN_PHONE, reg.getPhone());
        values.put(COLUMN_MESSAGE, reg.getMessage());
        db.insert(TABLE_REG, null, values);
        db.close();


    }
    public String databaseToString(){
        String dbString = "";
        SQLiteDatabase db = getReadableDatabase();
        String query = "SELECT name FROM "+TABLE_REG;
        Cursor c = db.rawQuery(query, null);
        c.moveToFirst();

        while (!c.isAfterLast()) {
            if (c.getString(c.getColumnIndex("name"))!= null){
                dbString += c.getString(c.getColumnIndex("name"));
                dbString += "\n";
            }

        }
        db.close();
        return dbString;
    }
}
Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
amit
  • 29
  • 1
  • 6

1 Answers1

0

You code is correct only. Please try to uninstall and run your app (As suggested by RRR). This is happened because after the first run of the application only you might have added Message column in the create table query. On create of SQLiteOpenHelper will get call at the first time only (or it should be an upgrade).

Please try to view the db file of your application using any sqllite viewer - Find the links below

To Pull the Application DB file using adb

Sqlite viewer - software

SQLite Manager - Firefox plugin

SQLiteOpenHelper

Sudhi
  • 403
  • 4
  • 8