0

Android 2.3.3

static String name = "Database";
static int version = 1;

SQLiteDatabase sqlDB;
Cursor c;


public Database(Context context) {
    super(context, name, null, version);
    // TODO Auto-generated constructor stub

    sqlDB = getWritableDatabase();

    }

    @Override
    public void onCreate(SQLiteDatabase sqlDB) {
        // TODO Auto-generated method stub


    sqlDB.execSQL("create table if not exists Operations(Command Text, Pos1 Text, " +
                "Pos2 Text, Pos3 Text, Pos4 Text, Pos5 Text, Pos6 Text, Pos7 Text, Pos8 Text, Pos9 Text, Pos10 Text)");


    }

public void insertData() {
        // TODO Auto-generated method stub

        String addQuery ="ADD, add, and, had, ad, anddd, anndd, null, null, null, null";

                sqlDB.execSQL("insert into Operations values("+ addQuery +")"); 
                     //Exception at the above line
}

There are a total of 11 columns.

Can i insert NULL into the database? If yes, is there any problem with the way i am trying to insert the data into the database?

Here is the error that i am getting :::

12-21 14:53:29.629: E/AndroidRuntime(11894): Caused by: android.database.sqlite.SQLiteException: near "ADD": syntax error: insert into Operations values(ADD, add, and, had, ad, anddd, anndd, null, null, null, null)
12-21 14:53:29.629: E/AndroidRuntime(11894):    at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
12-21 14:53:29.629: E/AndroidRuntime(11894):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1765)
12-21 14:53:29.629: E/AndroidRuntime(11894):    at com.x.x.Database.insertData(Database.java:71)
12-21 14:53:29.629: E/AndroidRuntime(11894):    at com.x.x.xxx.onCreate(xxx.java:109)
12-21 14:53:29.629: E/AndroidRuntime(11894):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-21 14:53:29.629: E/AndroidRuntime(11894):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-21 14:53:29.629: E/AndroidRuntime(11894):    ... 11 more

can someone help me why i am getting this error with the syntax?

4 Answers 4

1

Strings should be inclused in '' so change String addQuery ="ADD, add, and, had, ad, anddd, anndd, null, null, null, null";

to:

String addQuery ="'ADD', 'add', 'and', 'had', 'ad', 'anddd', 'anndd', null, null, null, null";
Sign up to request clarification or add additional context in comments.

Comments

0

SQL syntax error, please use insert into test(aa,bb) values('ADD','BB') which aa is your field name and 'ADD' is string

Comments

0

According to me as you have defined a string (addQuery) with 11 value but it is counting it as a one. I'm sure there is a problem that's why you are getting runtime error.

1 Comment

Yeah, It took the entire assignment as a string. Got it solved.
0
 //  Possible to Get Error 
    public void insertData() {
            // TODO Auto-generated method stub

            String addQuery ="ADD, add, and, had, ad, anddd, anndd, null, null, null, null";

                    sqlDB.execSQL("insert into Operations values("+ addQuery +")"); 
                         //Exception at the above line
    }

    use Like Below 

    public void insertData() {

    ContentValues cv=new ContentValues();
    cv.put("column1,value);
    cv.put("column2,value);
    cv.put("column3,value);
    cv.put("column4,value);
    cv.put("column5,value);
    cv.put("column7,value);
     database.insert(tabName, SINGLE_colName, cv);

    }

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.