mark.bendal.erica mark.bendal.erica - 2 months ago 9
Android Question

Android IHTTPSession parms data save to SQLite

I want to save the data coming from the web browser to SQLite database

here is my code

WebServer.java

public class WebServer extends NanoHTTPD {

public WebServer(int port) {
super(port);
}

public Response serve(IHTTPSession session) {
String msg = "<html><body><h1>SERVER</h1>\n";
Map<String, String> parms = session.getParms();
if (parms.get("item_name") == null && parms.get("quantity") == null && parms.get("instruction") == null && parms.get("pos_id") == null) {
msg += "<form action='?' method='get'>\n " +
"<p>ITEM NAME : <input type='text' name='item_name'></p>\n" +
"<p>QUANTITY : <input type='text' name='quantity'></p>\n" +
"<p>INSTRUCTION : <input type='text' name='instruction'></p>\n" +
"<p>POS ID : <input type='text' name='pos_id'></p>\n" +
"<p> <input type='submit'> </p> \n" +
"</form>\n";
} else {
msg += "<p>ITEM NAME :, " + parms.get("item_name") + "!</p>\n";
msg += "<p>QUANTITY :, " + parms.get("quantity") + "!</p>\n";
msg += "<p>INSTRUCTION :, " + parms.get("instruction") + "!</p>\n";
msg += "<p>POS ID :, " + parms.get("pos_id") + "!</p>\n";
}
return newFixedLengthResponse( msg + "</body></html>\n" );
}

}


And i already created the Database Helper which the Database will be created

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "TableOrder.db";
public static final String TABLE_NAME = "Table_order";
public static final int DB_VERSION = 1;
public static final String COL_1 = "ID";
public static final String COL_2 = "Item_Name";
public static final String COL_3 = "Quantity";
public static final String COL_4 = "Instruction";
public static final String COL_5 = "POS_ID";

public DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, DB_VERSION);
SQLiteDatabase db = this.getWritableDatabase();
}


@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("create table " + TABLE_NAME + "ID INTEGER PRIMARY KEY AUTOINCREMENT, Item_Name TEXT," +
"Quantity INT, Instruction TEXT, POS_ID INT");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);

}



}


I want the "names" of the html codes on the WebServer.java will be saved to the on SQLite after clicking the submit button

Any help will be much appreciated

Thank you

Answer

You should add the insertData function like this

public boolean insertData(String item_name, String qty, String instruction, String posid, String stat) {

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2,item_name);
        contentValues.put(COL_3,qty);
        contentValues.put(COL_4,instruction);
        contentValues.put(COL_5,posid);
        contentValues.put(COL_6,stat);
        long result = db.insert(TABLE_NAME,null,contentValues);
        if(result == -1) {
            return false;
        }
        else {
            return true;
        }
    }

and after adding the insert data go to the weberserver.java and add this code

  if (parms.get("item_name") == null && parms.get("quantity") == null && parms.get("instruction") == null && parms.get("pos_id") == null) {
        msg += "<form action='?' method='get'>\n  " +
                "<p>ITEM NAME : <input type='text' name='item_name'></p>\n" +
                "<p>QUANTITY : <input type='text' name='quantity'></p>\n" +
                "<p>INSTRUCTION : <input type='text' name='instruction'></p>\n" +
                "<p>POS ID : <input type='text' name='pos_id'></p>\n" +
                "<p> <input type='submit'> </p> \n";

        //SQLite insert
        MainActivity.myDb.insertData(itname,qty,instruc,pid,"1");