mercredi 29 avril 2015

passing data into internal database

i have managed to create a application that grabs a json array from a website and converts it into several strings, but now i need to create a internal database so that i can store that data.

the json for example would be

{"tblTuts":[{"tutID":"00001","catID":"00002","tutTitle":"edsfadfas","userID":"00002","tutDateCreated":"0000-00-00 00:00:00","tutDateModified":"2015-04-28 12:25:29","tutText":"fdsa","imageID":"00007","videoID":"00001"},{"tutID":"00002","catID":"00001","tutTitle":"html","userID":"00001","tutDateCreated":"2015-04-27 23:32:58","tutDateModified":"2015-04-27 23:32:58","tutText":"sfdvdsvsd","imageID":"00008","videoID":"00004"},{"tutID":"00003","catID":"00002","tutTitle":"pie","userID":"00002","tutDateCreated":"2015-04-28 12:27:01","tutDateModified":"2015-04-28 12:27:28","tutText":"pie is fun","imageID":"00007","videoID":"00001"}],"success":1}

it from their converts them into a string after being passed through a json parser

private static final String TAG_TUTID = "tutID";
private static final String TAG_TUTTITLE = "tutTitle";
private static final String TAG_NAME = "tutText";
private static final String TAG_DATEC = "tutDateCreated";
private static final String TAG_DATEM = "tutDateModified";
private static final String TAG_IMAGE = "imageID";
private static final String TAG_VIDEO = "videoID";

this is my current database controller as far as i have managed to figure out.

public class DBController  extends SQLiteOpenHelper {

public DBController(Context applicationcontext) {
    super(applicationcontext, "androidsqlite.db", null, 1);
}
//Creates Table
@Override
public void onCreate(SQLiteDatabase database) {
    String query;
    query = "CREATE TABLE tblTuts ( tutID INTEGER PRIMARY KEY, catID TEXT, tutTitle TEXT, userID TEXT, tutDateCreated TEXT, tutDateModified TEXT, tutText TEXT, imageID TEXT, videoID TEXT)";
    database.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
    String query;
    query = "DROP TABLE IF EXISTS tblTuts";
    database.execSQL(query);
    onCreate(database);
}
/**
 * Inserts User into SQLite DB
 * @param queryValues
 */
public void insertUser(HashMap<String, String> queryValues) {
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("tutID", queryValues.get("tutID"));
    values.put("catID", queryValues.get("catID"));
    values.put("tutTitle", queryValues.get("tutTitle"));
    values.put("tutDateCreated", queryValues.get("tutDateCreated"));
    values.put("tutDateModified", queryValues.get("tutDateModified"));
    values.put("tutText", queryValues.get("tutText"));
    values.put("imageID", queryValues.get("imageID"));
    values.put("videoID", queryValues.get("videoID"));
    database.insert("tblTuts", null, values);
    database.close();
}

/**
 * Get list of Users from SQLite DB as Array List
 * @return
 */
public ArrayList<HashMap<String, String>> getAllUsers() {
    ArrayList<HashMap<String, String>> wordList;
    wordList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT  * FROM tblTuts";
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("tutID", cursor.getString(0));
            map.put("catID", cursor.getString(1));
            map.put("tutTitle", cursor.getString(2));
            map.put("tutDateCreated", cursor.getString(3));
            map.put("tutDateModified", cursor.getString(4));
            map.put("tutText", cursor.getString(5));
            map.put("imageID", cursor.getString(6));
            map.put("videoID", cursor.getString(7));

            wordList.add(map);
        } while (cursor.moveToNext());
    }
    database.close();
    return wordList;
}

can someone help me figure out how to pass those strings and turn it into a working internal database?

Aucun commentaire:

Enregistrer un commentaire