1

I'am getting data from the server as a JSONObject and am saving to my table.

I'am facing the following error.

11-22 02:18:35.489: E/AndroidRuntime(2597): android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: create table Conntact (null INTEGER PRIMARY KEY AUTOINCREMENT, null TEXT, null TEXT, null TEXT, null TEXT, null TEXT, null TEXT);

for(int i=0;i<contactdata.length();i++)
        {
            JSONObject cont = contactdata.getJSONObject(i);
            String con_id = getValue(cont, "id");
            System.out.println("contactid"+con_id);
            String first_name = getValue(cont, "first_name");
            String last_name = getValue(cont, "last_name");
            String short_name = getValue(cont, "short_name");
            String pid = getValue(cont, "pid");
            String cid = getValue(cont, "cid");
            String number = getValue(cont, "914424411295");
            Conntact con = new Conntact(getActivity());
            con.insertValues(con_id, short_name, first_name, last_name, number, pid, cid);
            Toast.makeText(ctx, "contact inside", Toast.LENGTH_SHORT).show();
        }

This is my Conntact class

public class Conntact {

public Conntact() {

    // TODO Auto-generated constructor stub
}
static SQLiteDatabase db;
static ContactDB dedb;
public Conntact(Context context)
{
    dedb = new  ContactDB(context);
}
public static SQLiteDatabase open()
{
    return dedb.getWritableDatabase();
}
public void close()
{
    dedb.close();
}
public void insertValues(String con_id, String short_name,String first_name,String last_name,String pid,String cid,String number)
{
    db = open();
    ContentValues values = new ContentValues();
    values.put(ContactDB.con_id, con_id);
    values.put(ContactDB.short_name, short_name);
    values.put(ContactDB.first_name, first_name);
    values.put(ContactDB.last_name, last_name);
    values.put(ContactDB.pid, pid);
    values.put(ContactDB.cid, cid);
    values.put(ContactDB.number, number);
    db.insert(ContactDB.TABLE_NAME, null, values);
    this.close();
}

public ArrayList<String> getValues()
{
    ArrayList<String> names = new ArrayList<String>();
    db = open();
    String query = "select * from "+ContactDB.TABLE_NAME;
    Cursor cursor = db.rawQuery(query, null);
    if(cursor.moveToFirst())
    {
        do{
            names.add(cursor.getString(cursor.getColumnIndex(ContactDB.con_id)));
            names.add(cursor.getString(cursor.getColumnIndex(ContactDB.number)));
            names.add(cursor.getString(cursor.getColumnIndex(ContactDB.first_name)));
            names.add(cursor.getString(cursor.getColumnIndex(ContactDB.last_name)));
            names.add(cursor.getString(cursor.getColumnIndex(ContactDB.short_name)));;
            names.add(cursor.getString(cursor.getColumnIndex(ContactDB.pid)));;
            //names.add(cursor.get)
        }while(cursor.moveToNext());
    }
    return names;

}
public static  boolean isAvailable(String number)
{
    db = open();
    String query = "select * from"+ContactDB.TABLE_NAME+"where"+ContactDB.number+"="+number;
    Cursor c =db.rawQuery(query, null);
    if(c==null)
    {
        return false;
    }else
    return true;
}
}

This is my ContactDB class

public class ContactDB extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "AA_DB_NAME";
public static final String TABLE_NAME = "Conntact";
public static final int VERSION = 1;
public ContactDB(Context context) {
    super(context, DATABASE_NAME, null, VERSION);
    // TODO Auto-generated constructor stub
}

public static String con_id;
public static String short_name;
public static String first_name;
public static String last_name;
public static String pid;
public static String cid;
public static String number;
public static String CREATE_QUERY = "create table "+TABLE_NAME+" ("+con_id+" INTEGER PRIMARY KEY AUTOINCREMENT, "+short_name+" TEXT, "+first_name+" TEXT, "+last_name+" TEXT, "+pid+" TEXT, "+cid+" TEXT, "+number+" TEXT);";
@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(CREATE_QUERY);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE "+TABLE_NAME+" IF NOT EXISTS");
}

}

I don't know where i did mistakes. Please help if i go in a wrong way.

1
  • 1
    You need to assign values to the public static Strings in the ContactDB class. You should also make them final. Commented Nov 22, 2014 at 7:52

2 Answers 2

1

As mentioned, you need to assign values to the public static Strings in the ContactDB class. You should also make them final, to prevent them from being accidentally modified.

public static final String con_id = "con_id";
public static final String short_name = "short_name";
public static final String first_name = "first_name";
public static final String last_name = "last_name";
public static final String pid = "pid";
public static final String cid = "cid";
public static final String number = "number";   
Sign up to request clarification or add additional context in comments.

Comments

0
public static String CREATE_QUERY = "create table "+TABLE_NAME+" ("+con_id+" INTEGER PRIMARY KEY AUTOINCREMENT, "+short_name+" TEXT, "+first_name+" TEXT, "+last_name+" TEXT, "+pid+" TEXT, "+cid+" TEXT, "+number+" TEXT);";

your con_id is gettint null value. you can use hard coded string like "id"

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.