Sqlite Database Store Simple
Sqlite Database Store Simple
Database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
@Override
public void onCreate(SQLiteDatabase db) {
/*----------------------------------------------
create----------------------------------------------*/
db.execSQL("create table customers (id integer primary key Autoincrement ,"
+
"name text not null," +
"email text not null ," +
"password text not null, " +
"birth_date text not null ," +
"phone_number text not null," +
"gender text," +
"jop text)");
/*----------------------------------------------
Insert----------------------------------------------*/
db.execSQL("INSERT INTO categories (name,image) VALUES
('laptops','laptop1')");
db.execSQL("INSERT INTO categories (name,image) VALUES
('phone','mobile')");
db.execSQL("INSERT INTO categories (name,image) VALUES
('screen','monitor')");
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
/*-----------------------------------------------user table
functions-----------------------------------------------*/
public void signUp( String userName, String email, String password, String
BirthDate, String phone) {
techStore = getWritableDatabase();
ContentValues row = new ContentValues();
row.put("name", userName);
row.put("email", email);
row.put("password", password);
row.put("birth_date", BirthDate);
row.put("phone_number", phone);
row.put("gender", "");
row.put("jop", "");
techStore.insert("customers", null, row);
techStore.close();
}
public String ValidateUser(String email, String password, String phone, int
status) {
String validation = "Not Valid";
boolean isEmailFound = false;
boolean isPasswordFound = false;
boolean isPhoneFound=false;
techStore= this.getReadableDatabase();
String[] data = {email};
Cursor cursor = techStore.rawQuery("SELECT email,password,phone_number FROM
customers WHERE email= ?", data);
if (cursor.getCount()>0) {
cursor.moveToFirst();
if (cursor.getString(0).equalsIgnoreCase(email))
isEmailFound = true;
if (cursor.getString(1).equalsIgnoreCase(password))
isPasswordFound = true;
if (cursor.getString(2).equalsIgnoreCase(phone))
isPhoneFound = true;
}
cursor.close();
techStore.close();
if(status==1) {
if (isEmailFound == true && isPasswordFound == true)
validation = "Valid";
else if (isEmailFound == true && isPasswordFound == false)
validation = "Wrong Password";
}
else if(status==2) {
if (isEmailFound == true && isPhoneFound == true)
validation = "Valid for Recover";
else if (isEmailFound == true && isPhoneFound == false)
validation = "Wrong Phone Number";
}
return validation;
}
return cursor;
}
public boolean checkemail(String email) {
boolean isFound=false;
techStore= this.getReadableDatabase();
Cursor cursor = techStore.rawQuery("SELECT * FROM customers WHERE email=
'"+email+"'", null);
if (cursor.getCount()>0) {
cursor.moveToFirst();
isFound=true;
}
cursor.close();
techStore.close();
return isFound;
}
/*---------------------------------------------------------------------------------
---------------------------------*/
/*-----------------------------------------------products table
functions-----------------------------------------------*/
public Cursor returnProductsByCategory(int Category) {
techStore = getReadableDatabase();
String[] data={String.valueOf(Category)};
Cursor curs = techStore.rawQuery("SELECT * FROM products WHERE categoryId
=? ",data);
if (curs!=null)
curs.moveToFirst();
techStore.close();
return curs;
}
public Cursor returnBestSeller() {
techStore = getReadableDatabase();
Cursor curs = techStore.rawQuery("SELECT * ,max(best_seller) FROM products
group by categoryId ;",null);
if (curs!=null)
curs.moveToFirst();
techStore.close();
return curs;
}
public Cursor returnProductById(int id) {
techStore = getReadableDatabase();
String[] data={String.valueOf(id)};
Cursor curs = techStore.rawQuery("SELECT * FROM products WHERE id =?
",data);
if (curs!=null)
curs.moveToFirst();
techStore.close();
return curs;
}
public Cursor SearchByName(String Name) {
techStore = getReadableDatabase();
Cursor curs = techStore.rawQuery("SELECT * FROM products WHERE name Like
'%" + Name + "%'", null);
if (curs!=null) {
curs.moveToFirst();
}
techStore.close();
return curs;
}
/*---------------------------------------------------------------------------------
-------------------------------------*/
/*-----------------------------------------------categories table
functions-----------------------------------------------*/
public Cursor returnCategories() {
techStore = getReadableDatabase();
Cursor curs = techStore.rawQuery("SELECT * FROM categories ",null);
if (curs!=null)
curs.moveToFirst();
techStore.close();
return curs;
}
/*---------------------------------------------------------------------------------
---------------------------------------*/
/*-----------------------------------------------Order table
functions-----------------------------------------------*/
public boolean AddItem(int id,String email) {
boolean isFound=false;
Cursor cursor= getUserByEmail(email);
int userId=cursor.getInt(0);
cursor.close();
int orderId=checkOrder(userId);
if(orderId==-1)
{
addOrder(userId);
orderId=checkOrder(userId);
}
if(!checkItem(id,orderId)) {
techStore = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("order_id", orderId);
values.put("product_id", id);
values.put("quantity", 1);
techStore.insert("order_details", null, values);
techStore.close();
}
else
isFound=true;
return isFound;
}
public int checkOrder(int id) {
int orderId=-1;
techStore= this.getReadableDatabase();
Cursor cursor = techStore.rawQuery("SELECT * FROM orders WHERE user_id=
"+id+" and submit='waiting'", null);
if (cursor.getCount()>0) {
cursor.moveToFirst();
orderId = cursor.getInt(0);
}
cursor.close();
techStore.close();
return orderId;
}
return curs;
}
public Cursor returnSubmitedOrders(int orderId) {
techStore = getReadableDatabase();
Cursor curs = techStore.rawQuery("SELECT * FROM products Inner join
order_details on order_details.product_id = products.id Inner join orders on
order_details.order_id = orders.id WHERE orders.submit = 'done' and
orders.id="+orderId+"",null );
if (curs!=null)
curs.moveToFirst();
techStore.close();
return curs;
}
public Cursor returnWaitingOrders(int orderId) {
techStore = getReadableDatabase();
Cursor curs = techStore.rawQuery("SELECT * FROM products Inner join
order_details on order_details.product_id = products.id Inner join orders on
order_details.order_id = orders.id WHERE orders.submit = 'waiting' and
orders.id="+orderId+"",null );
if (curs!=null)
curs.moveToFirst();
techStore.close();
return curs;
}
bestSellerUpdate(id);
techStore=getWritableDatabase();
ContentValues values = new ContentValues();
values.put("address",address);
values.put("total_price", String.valueOf(totalPrice));
values.put("submit","done");
techStore.update("orders", values,"id= "+id,null );
techStore.close();
}
public void bestSellerUpdate(int orderId)
{
Cursor cursor=returnWaitingOrders(orderId);
techStore=getWritableDatabase();
ContentValues values;
for(int i=0;i<cursor.getCount();i++)
{
values= new ContentValues();
values.put("quantity",(cursor.getInt(4)-cursor.getInt(9)));
values.put("best_seller",(cursor.getInt(6)+cursor.getInt(9)));
techStore.update("products", values,"id= "+cursor.getInt(0),null );
cursor.moveToNext();
}
techStore.close();
}
/*---------------------------------------------------------------------------------
----------------------------------*/