0

hey i created my database and want to insert some fields init but getting an sqliteexception no such table and while compiling cannot insert into the table.getting the id value -1 where i am not getting the exact error on it.

public long insertData(String name, String number, String email, String jobtype, String uri, String msg)
{
    SQLiteDatabase db = database.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(database.NAME, name);
    contentValues.put(database.NUMBER, number);
    contentValues.put(database.EMAIL, email);
    contentValues.put(database.JOBTYPE, jobtype);
    contentValues.put(database.URI, uri);
    contentValues.put(database.MSG, msg);
    long id = db.insert(database.TABLE_NAME, null, contentValues);
    return id;
}

 static class Database extends SQLiteOpenHelper{

    private static final String DATABASE_NAME="cloudicadatabase";
    private static final String TABLE_NAME="CLTABLE";
    private static final int DATABASE_VERSION= 1;
    private static final String COLUMN_UID = "_id";
    private static final String NAME="Name";
    private static final String NUMBER="Number";
    private static final String EMAIL="Email";
    private static final String JOBTYPE="Jobtype";
    private static final String URI="Uri";
    private static final String MSG="Msg";
    private static final String CREATE_TABLE = "CREATE TABLE" + TABLE_NAME + "(" +
            COLUMN_UID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
            NAME + "VARCHAR(32) NOT NULL, " +
            NUMBER + "INTEGER, " +
            EMAIL + "VARCHAR(25) NOT NULL, " +
            JOBTYPE + "VARCHAR(20) NOT NULL, " +
            URI + "VARCHAR(50) NOT NULL, " +
            MSG + "VARCHAR(500) NOT NULL);";
    private static final String DROP_TABLE = "DROP TABLE "+TABLE_NAME+" IF EXISTS";

    private Context context;
    public Database(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        try {
            db.execSQL(CREATE_TABLE);
        } catch (SQLException e) {
            Message.message(context, ""+e);
        }

    }
main.java

public void submit(View view)
{
    String nam = name.getText().toString();
    String numb = num.getText().toString();
    String mail = email.getText().toString();
    String jtype = job.getText().toString();
    String ur = uri.getText().toString();
    String msag = msg.getText().toString();

    long id = database.insertData(nam, numb, mail, jtype, ur, msag);

    if (id < 0)
    {
        Message.message(this, "Unsucessful");
    }
    else {
        Message.message(this, "Submited");
    }

logcat

        no such table: CLTABLE
    12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: Error inserting Jobtype=dfg Msg=gfdgfd Number=543 Uri=gfd Email=fdgf Name=sau
    12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: android.database.sqlite.SQLiteException: no such table: CLTABLE (code 1): , while compiling: INSERT INTO CLTABLE(Jobtype,Msg,Number,Uri,Email,Name) VALUES (?,?,?,?,?,?)
    12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase:     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase:     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
    12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase:     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
    12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase:     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase:     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase:     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
    12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase:     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
    12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase:     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
    12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase:     at com.sun.suni.design.DataAdapter.insertData(DataAdapter.java:30)
    12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase:     at com.sun.suni.design.Details.submit(Details.java:36)
    12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase:     at java.lang.reflect.Method.invoke(Native Method)
    12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase:     at java.lang.reflect.Method.invoke(Method.java:372)

2 Answers 2

7

Your table is not created so you got NPE.

You should add white space between Column Name and Column Type

 private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" +
        COLUMN_UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        NAME + " VARCHAR(32) NOT NULL, " +
        NUMBER + " INTEGER, " +
        EMAIL + " VARCHAR(25) NOT NULL, " +
        JOBTYPE + " VARCHAR(20) NOT NULL, " +
        URI + " VARCHAR(50) NOT NULL, " +
        MSG + " VARCHAR(500) NOT NULL);";
Sign up to request clarification or add additional context in comments.

4 Comments

sqlitestudio.pl There are plenty of tools out there for checking SQL commands, you should always output sql commands to strings and see if they work in tools like this. If they don't then your command has issues.
I have given spaces but no change getting same issue
@sun uninstall & run again , if not works post your logcat
yeah i got it thank you @M D and is it necessary to uninstall every time when i am inserting a new record into it
0

You are missing a space after "CREATE TABLE " :

private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" +
            COLUMN_UID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
            NAME + "VARCHAR(32) NOT NULL, " +
            NUMBER + "INTEGER, " +
            EMAIL + "VARCHAR(25) NOT NULL, " +
            JOBTYPE + "VARCHAR(20) NOT NULL, " +
            URI + "VARCHAR(50) NOT NULL, " +
            MSG + "VARCHAR(500) NOT NULL);";

Comments

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.