Last active
August 29, 2015 14:05
-
-
Save malvre/bb87d1f8470e288d2e71 to your computer and use it in GitHub Desktop.
[Android] SQLite ridiculous
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Usage: | |
ArrayList<ContentValues> rows = DB.selectRows(this, "SELECT * FROM users WHERE active = ?", new String[] { strActive }); | |
DB.executeSQL(this, | |
"INSERT INTO users (name, active, email) VALUES (?, ?, ?)", | |
new String[]{ name, active, email }); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.DatabaseUtils; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import java.util.ArrayList; | |
public class DB extends SQLiteOpenHelper { | |
private static String TAG = DB.class.getSimpleName(); | |
private static SQLiteDatabase mInstance = null; | |
private static int DATABASE_VERSION = 1; | |
private static String DATABASE_NAME = "database_name"; | |
public static final String CREATE_TABLE_SCRIPT = "CREATE TABLE bla bla bla"; | |
public synchronized static SQLiteDatabase instance(Context ctx) { | |
if (mInstance == null) { | |
mInstance = new DB(ctx.getApplicationContext()).getWritableDatabase(); | |
} | |
return mInstance; | |
} | |
private DB(Context ctx) { | |
super(ctx, DATABASE_NAME, null, DATABASE_VERSION); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
db.execSQL(CREATE_TABLE_SCRIPT); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
db.execSQL("DROP TABLE IF EXISTS tablename"); | |
onCreate(db); | |
} | |
public static ArrayList<ContentValues> selectRows(Context ctx, String sql, String[] params) { | |
Cursor c = DB.instance(ctx).rawQuery(sql, params); | |
ArrayList<ContentValues> retVal = new ArrayList<ContentValues>(); | |
ContentValues map; | |
if(c.moveToFirst()) { | |
do { | |
map = new ContentValues(); | |
DatabaseUtils.cursorRowToContentValues(c, map); | |
retVal.add(map); | |
} while(c.moveToNext()); | |
} | |
c.close(); | |
return retVal; | |
} | |
public static void executeSQL(Context ctx, String sql, String[] params) { | |
DB.instance(ctx).execSQL(sql, params); | |
} | |
public static long lastId(Context ctx, String tabela) { | |
ArrayList<ContentValues> rows = DB.selectRows(ctx, "SELECT max(_id) as seq FROM " + tabela, null); | |
return rows.get(0).getAsLong("seq"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment