Created
June 2, 2012 20:41
-
-
Save gehaxelt/2859864 to your computer and use it in GitHub Desktop.
Databasehelperclass for SQLite support in android
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
package in.gehaxelt.sqlinjection; | |
import java.util.ArrayList; | |
import java.util.List; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
public class Databasehelper extends SQLiteOpenHelper { | |
static final String DBNAME = "sqlinjection" ; | |
static final int DBVERSION = 1; | |
static final String TABLENAME = "sqlinjection" ; | |
static final String COLID = "ID" ; | |
static final String COLTYPE = "TYPE" ; | |
static final String COLDATE = "DATE" ; | |
static final String COLTIME = "TIME" ; | |
public Databasehelper(Context context) { | |
super(context, DBNAME, null, DBVERSION); | |
// TODO Auto-generated constructor stub | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
// TODO Auto-generated method stub | |
db.execSQL("CREATE TABLE "+TABLENAME+"( "+COLID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "+COLTYPE+" TEXT, "+ COLDATE + " VARCHAR(10), "+ COLTIME + " VARCHAR (5))"); | |
db.execSQL("INSERT INTO "+TABLENAME+" (TYPE,DATE,TIME) VALUES ('testtype','testdate','testtime')"); | |
db.execSQL("INSERT INTO "+TABLENAME+" (TYPE,DATE,TIME) VALUES ('testtype2','testdate2','testtime2')"); | |
db.execSQL("INSERT INTO "+TABLENAME+" (TYPE,DATE,TIME) VALUES ('testtype3','testdate3','testtime3')"); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
// TODO Auto-generated method stub | |
db.execSQL("DROP TABLE "+ TABLENAME+" "); | |
onCreate(db); | |
} | |
public String getID(String id){ | |
String selectQuery = "SELECT TYPE FROM " + TABLENAME+ " where ID = "+id; | |
SQLiteDatabase db = this.getReadableDatabase(); | |
Cursor cursor = db.rawQuery(selectQuery, null); | |
if (cursor.moveToFirst()) { | |
db.close(); | |
return cursor.getString(0); | |
} | |
return ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment