Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8965113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:44:35+00:00 2026-06-15T16:44:35+00:00

I trying to create SQLiteDatabase for keeping my data which I get from JSONObject

  • 0

I trying to create SQLiteDatabase for keeping my data which I get from JSONObject from post request.
My main class:

public class MainActivity extends Activity {

    static JSONObject result;
    public Context mContext;
    public SQLiteDatabase db;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mContext = getApplicationContext();
       new UpdateData().execute();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private class UpdateData extends AsyncTask<String, Void, JSONObject>{

        private JSONObject object;
            @Override
            protected JSONObject doInBackground(String... params) {
                try {
                    HttpClient client = new DefaultHttpClient();  
                    String postURL = "http://test.com";
                    HttpPost post = new HttpPost(postURL);
                        List<NameValuePair> crc = new ArrayList<NameValuePair>();
                        crc.add(new BasicNameValuePair("crc", "test"));
                        UrlEncodedFormEntity ent = new UrlEncodedFormEntity(crc,HTTP.UTF_8);
                        post.setEntity(ent);
                        HttpResponse responsePOST = client.execute(post);  
                        HttpEntity entit = responsePOST.getEntity();
                        String retSrc = EntityUtils.toString(entit);
                        object = new JSONObject(retSrc);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return object;
            }
            @Override
            protected void onPostExecute(JSONObject result) {
                MainActivity.this.result = result;
                DataBase dbHelper = new DataBase(mContext) ;
                db = dbHelper.getWritableDatabase(); 
                dbHelper.createDB(db, result);  
                /*Cursor cursor = db.query("departments", null , null,
                        null, null, null, null, null);
                System.out.println(cursor.getString(1));*/
                }

    }
}

and DataBaseHelper

public class DataBaseHelper extends SQLiteOpenHelper {

    public boolean isDownloaded = false; 
    public boolean shutdown = false;

    private Context mContext;
    private JSONObject exams;
    private JSONObject specs;
    private JSONObject deps;

    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "data.db";

    private static final String EXAMS_TABLE_NAME = "exams";
    private static final String EXAMS_TABLE_CREATE =
                "CREATE TABLE " + EXAMS_TABLE_NAME + " (" +
                "id VARCHAR (255), " +              //0
                "name VARCHAR (255)," +             //1
                "type VARCHAR (100)," +             //2
                "level VARCHAR (100),"  + ");" ;    //3


    private static final String SPEC_TABLE_NAME = "specializations";
    private static final String SPEC_TABLE_CREATE =
                "CREATE TABLE " + SPEC_TABLE_NAME + " (" +
                "id VARCHAR (255), " +                  //0
                "name VARCHAR (255)," +                 //1
                "name_en VARCHAR (255)," +          //2
                "description VARCHAR (255)," + ");" ;       //3

    private static final String DEP_TABLE_NAME = "departments";
    private static final String DEP_TABLE_CREATE =
                "CREATE TABLE " + DEP_TABLE_NAME + " (" +
                "id VARCHAR (255), " +                  //0
                "name VARCHAR (255)," +                 //1
                "name_en VARCHAR (255)," +          //2
                "www VARCHAR (255)," +                  //3
                "email VARCHAR (255)," +                //4
                "phone VARCHAR (100),"  + ");" ;        //5



    public DataBase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mContext = context;
    }

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

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

    public boolean doesDBexist() {
        File db;
        db = new File("/data/data/my.package/databases/data.db");
        return db.exists();
    }

    public boolean createDB(SQLiteDatabase db, JSONObject jsonObject) {
        try { // if not working create new one
            exams = jsonObject.getJSONObject("exams");
            specs = jsonObject.getJSONObject("spec");
            deps = jsonObject.getJSONObject("department");
            db.execSQL(EXAMS_TABLE_CREATE);             // here I get exception (described below)
            db.execSQL(SPEC_TABLE_CREATE);
            db.execSQL(DEP_TABLE_CREATE);
            putContentValues(db);
            isDownloaded = true;
            return true;
        } catch (SQLiteException e1) {
            db = null;
            return false;
        } catch (Exception e3) {
            shutdown = true;
            return false;
        }
    }


        public void putContentValues(SQLiteDatabase db) throws JSONException    {
            ContentValues cv = new ContentValues();

            for (Iterator<String> it = exams.keys(); it.hasNext(); it.next()) {
                JSONObject exam = exams.getJSONObject(it.toString());
                cv.put("id", it.toString());
                cv.put("name", exam.getString("name"));
                cv.put("type", exam.getString("type"));
                cv.put("level", exam.getString("level"));
                db.insert(EXAMS_TABLE_NAME, null, cv);
                cv.clear();

            }

            for (Iterator<String> it = specs.keys(); it.hasNext(); it.next()) {
                JSONObject spec = specs.getJSONObject(it.toString());
                cv.put("id", it.toString());
                cv.put("name", spec.getString("name"));
                cv.put("name_en", spec.getString("name_en"));
                cv.put("desription", spec.getString("desription"));

                db.insert(SPEC_TABLE_NAME, null, cv);
                cv.clear();
            }

            for (Iterator<String> it = deps.keys(); it.hasNext(); it.next()) {
                JSONObject dep = deps.getJSONObject(it.toString());
                cv.put("id", it.toString());
                cv.put("name", dep.getString("name"));
                cv.put("name_en", dep.getString("name_en"));
                cv.put("www", dep.getString("www"));
                cv.put("email", dep.getString("email"));
                cv.put("phone", dep.getString("phone"));
                db.insert(DEP_TABLE_NAME, null, cv);
                cv.clear();
            }

        }

}

I can’t debug it. When I create Cursor in MainActivity, I get exception after line db.execSQL(EXAMS_TABLE_CREATE); android.database.sqlite.SQLiteException: no such table: departments: , while compiling: SELECT * FROM departments and when I don’t create cursor it’s just stops there while debugging. I can’t check if there is anything in my database and if it’s corectly made.
I tried to rename my database like someone said in this question :
android.database.sqlite.SQLiteException: no such table but it didn’t solve this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:44:36+00:00Added an answer on June 15, 2026 at 4:44 pm

    that cannot be correct:

    "CREATE TABLE " + EXAMS_TABLE_NAME + " (" +
                "id VARCHAR (255), " +              //0
                "name VARCHAR (255)," +             //1
                "type VARCHAR (100)," +             //2
                "level VARCHAR (100),"  + ");" ;
    

    remove the last “,” in that create-statement and try it again.

    btw… you did that fault in all create statements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a list adapter that pulls in and displays data from
I'm trying to create new database using SQLiteOpenHelper. @Override public void onCreate(SQLiteDatabase sqLiteDatabase) {
I am trying to create an application which requires a two table SQLite database
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Trying to create a new Dedicated Cache Role in Windows Azure but get the
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I am trying to write a query for retrieving data from SQLite database. I
I make a database with four columns. @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE
I'm trying to create image from SQLite database in Java Swing application. But it
I am trying to create an SQLite database. The create method: private static class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.