Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Insert, Select and Delete all table row data in SQLite database?

    • 0
    • 1
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 198
    Comment on it

    In the below example code I have clearly described how to insert,Select and delete all table row from the database. Here, first I have extended SQLiteOpenHelper class After this I have used onCreate method to create a database table. In next step, in last ArrayList class, I got getAllPlaces() from ListView Activity and there I had used selectQuery.

     

    Step(1)Created a DataBaseConroller class -

     

    public class DBController extends SQLiteOpenHelper {
            private static final String tablename = "places";  // tablename
            private static final String place = "place";  // column name
            private static final String id = "ID";  // auto generated ID column
            private static final String country = "country"; // column name
            private static final String databasename = "placesinfo"; // Dtabasename
            private static final int versioncode = 1; //versioncode of the database
    
         
    
            public DBController(Context context) {
                super(context, databasename, null, versioncode);
    
            }
            @Override
            public void onCreate(SQLiteDatabase database) {
                String query;
                query = "CREATE TABLE IF NOT EXISTS " + tablename + "(" + id + " integer primary key, " + place + " text, " + country + " text)";
                database.execSQL(query);
            }
    
    
           //--insertData into Database--
    
        public void insertData(String place_name,String country_name)
        {
            SQLiteDatabase db= this.getWritableDatabase();
            ContentValues initialValues = new ContentValues();
            initialValues.put(place,place_name);
            initialValues.put(country,country_name);
            db.insert("places", null, initialValues);
    
        }
    
           @Override
            public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
                String query;
                query = "DROP TABLE IF EXISTS " + tablename;
                database.execSQL(query);
                onCreate(database);
            }
    
        public void deleteAll()
        {
            SQLiteDatabase db= this.getWritableDatabase();
            String selectQuery=("delete from "+ tablename);
            db.execSQL(selectQuery);
        }
    
        
            public ArrayList<HashMap<String, String>> getAllPlace() {
                ArrayList<HashMap<String, String>> wordList;
                wordList = new ArrayList<HashMap<String, String>>();
                String selectQuery = "SELECT  * FROM " + tablename;
                SQLiteDatabase database = this.getWritableDatabase();
                Cursor cursor = database.rawQuery(selectQuery, null);
                if (cursor.moveToFirst()) {
                    do {
    
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("id", cursor.getString(0));
                        map.put("place", cursor.getString(1));
                        map.put("country", cursor.getString(2));
    
                        wordList.add(map);
                    } while (cursor.moveToNext());
                }
                return wordList;
            }
    }

     

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: