Created
September 28, 2014 21:23
-
-
Save Motoharujap/0d7657a88d8667200ef3 to your computer and use it in GitHub Desktop.
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 com.example.motoharu.shoplist; /** | |
* Created by Motoharu on 18.09.2014. | |
*/ | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.SQLException; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
public class DBhelper extends SQLiteOpenHelper { | |
// DATABASE INFORMATION | |
public static final String DB_NAME = "MEMBER.DB"; | |
public static final int DB_VERSION = 1; | |
//id for all | |
public static final String MAIN_ID = "_id"; | |
// MAIN TABLE INFORMATTION | |
public static final String TABLE_TITLES = "titles"; | |
public static final String TITLES_NAME = "name"; | |
public static final String TITLES_ID = "titles_id"; | |
// GOODS TABLE INFO | |
public static final String TABLE_GOODS = "goods"; | |
public static final String GOODS_NAME = "goodsName"; | |
public static final String GOODS_ID = "goods_id"; | |
//TAGS TABLE INFO | |
public static final String TABLE_TAGS = "tags"; | |
public static final String TAGS_ID = "tags_id"; | |
// MAIN TABLE CREATION STATEMENT | |
private static final String CREATE_TABLE_MAIN = "create table " | |
+ TABLE_TITLES + "(" | |
+ MAIN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " | |
+ TITLES_NAME + " TEXT);"; | |
// GOODS TABLE CREATION STATEMENT | |
private static final String CREATE_TABLE_GOODS = "create table " | |
+ TABLE_GOODS + "(" | |
+ MAIN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " | |
+ GOODS_NAME + " TEXT);"; | |
// TAGS TABLE CREATION STATEMENT | |
private static final String CREATE_TABLE_TAGS = "create table " | |
+ TABLE_TAGS + "(" | |
+ MAIN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " | |
+ TITLES_ID + " INTEGER, " | |
+ GOODS_ID + " INTEGER);"; | |
public DBhelper(Context context) { | |
super(context, DB_NAME, null,DB_VERSION); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
db.execSQL(CREATE_TABLE_MAIN); | |
db.execSQL(CREATE_TABLE_GOODS); | |
db.execSQL(CREATE_TABLE_TAGS); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
// TODO Auto-generated method stub | |
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TITLES); | |
db.execSQL("DROP TABLE IF EXISTS " + TABLE_GOODS); | |
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TAGS); | |
onCreate(db); | |
} | |
public Cursor getGoods(long rowId) throws SQLException { | |
SQLiteDatabase db = this.getReadableDatabase(); | |
Cursor mCursor = db.query(true, TABLE_GOODS, | |
new String[] {MAIN_ID, GOODS_NAME}, MAIN_ID + "=" + rowId, null, | |
null, null, null, null); | |
if (mCursor != null) { | |
mCursor.moveToFirst(); | |
} | |
return mCursor; | |
} | |
public Cursor getTitles(long rowId) throws SQLException { | |
SQLiteDatabase db = this.getReadableDatabase(); | |
Cursor mCursor = db.query(true, TABLE_TITLES, | |
new String[] {MAIN_ID, TITLES_NAME}, MAIN_ID + "=" + rowId, null, | |
null, null, null, null); | |
if (mCursor != null) { | |
mCursor.moveToFirst(); | |
} | |
return mCursor; | |
} | |
//filling up the GOODS_ID and TITLES_ID fields in TABLE_TAGS | |
public long createTags(long goods_id, long title_id) | |
{ | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues cv = new ContentValues(); | |
cv.put(TITLES_ID, title_id); | |
cv.put(GOODS_ID, goods_id); | |
long id = db.insert(TABLE_TAGS, null, cv); | |
return id; | |
} | |
//Creating an item in the GOODS table and assigning a tag to it | |
public long createGoodsValue(String name) | |
{ | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues initialValues = new ContentValues(); | |
initialValues.put(GOODS_NAME, name); | |
long goods_id = db.insert(TABLE_GOODS, null, initialValues); | |
db.close(); | |
return goods_id; | |
} | |
//creating a value in TITLES table, returning an id | |
public long createTitlesValue(String name) { | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues cv = new ContentValues(); | |
cv.put(TITLES_NAME, name); | |
long title_id = db.insert(TABLE_TITLES, null, cv); | |
return title_id; | |
} | |
//updating table titles | |
public boolean updateTitlesValue(long title_id, String name) { | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues updateValues = new ContentValues(); | |
updateValues.put(TITLES_NAME, name); | |
return db.update(TABLE_TITLES, updateValues, MAIN_ID + "=" + title_id, | |
null) > 0; | |
} | |
// updating table goods | |
public boolean updateGoodsValue(long goods_id, String name) | |
{ | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues updateValues = new ContentValues(); | |
updateValues.put(GOODS_NAME, name); | |
return db.update(TABLE_GOODS, updateValues, MAIN_ID + "=" + goods_id, null) > 0; | |
} | |
public void close() { | |
this.close(); | |
} | |
public Cursor getAllData() { | |
SQLiteDatabase database = this.getReadableDatabase(); | |
return database.query(DBhelper.TABLE_TITLES, null, null, null, null, null, null); | |
} | |
public Cursor getAllDataGoods() { | |
SQLiteDatabase database = this.getReadableDatabase(); | |
return database.query(DBhelper.TABLE_GOODS, null, null, null, null, null, null); | |
} | |
public Cursor readData() { | |
SQLiteDatabase database = this.getReadableDatabase(); | |
String[] allColumns = new String[] { DBhelper.MAIN_ID, | |
DBhelper.TITLES_NAME}; | |
Cursor c = database.query(DBhelper.TABLE_TITLES, allColumns, null, | |
null, null, null, null); | |
if (c != null) { | |
c.moveToFirst(); | |
} | |
return c; | |
} | |
public void deleteData(long memberID) { | |
SQLiteDatabase database = this.getWritableDatabase(); | |
database.delete(DBhelper.TABLE_TITLES, DBhelper.MAIN_ID + "=" | |
+ memberID, null); | |
} | |
public Cursor getAllTitles() { | |
SQLiteDatabase database = this.getWritableDatabase(); | |
return database.query(TABLE_TITLES, new String[]{MAIN_ID, | |
TITLES_NAME}, null, | |
null, null, null, null); | |
} | |
public Cursor getAllGoods() | |
{ | |
SQLiteDatabase database = this.getWritableDatabase(); | |
return database.query(TABLE_GOODS, new String[]{MAIN_ID, | |
GOODS_NAME}, null, | |
null, null, null, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment