How to parse new incoming message? - android

Am developing an android application in which i have created "Server" database. In that database i have two tables i,e employee_table and attendance_table in that i hav not included my onReceive function and with that onReceive function i need to parse the new message and that message content should be store in employee table and attendance table so please me out to solve this issue..
Thanks in advance..
Here is my DbHelper.java :
package com.example.attendancesystem;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbHelper extends SQLiteOpenHelper {
static String DATABASE_NAME="Server";
public static final String EmployeeTable="employee_table";
public static final String Name="name";
public static final String Mobile_no="mobile";
public static final String Id="id";
public static final String AttendanceTable="attendance_table";
public static final String Latitude="Lat";
public static final String Longitude="Lan";
public static final String Accuracy="Accry";
public static final String Date="date";
public static final String Time="time";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE="CREATE TABLE "+EmployeeTable+" ("+Id+" INTEGER PRIMARY KEY, "+Name+" TEXT, "+Mobile_no+" VARCHAR(13))";
db.execSQL(CREATE_TABLE);
CREATE_TABLE="CREATE TABLE "+AttendanceTable+" ("+Latitude+" double precision, "+Longitude+" double precision, "+Accuracy+" double,"+Date+" date,"+Time+"time)";
db.execSQL(CREATE_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+EmployeeTable);
db.execSQL("DROP TABLE IF EXISTS "+AttendanceTable);
onCreate(db);
}
}

Related

Android table creation error for multiple tables i thing on create method is went wrong somewhere

ONE TABLE I CAN CREATE THERE IS NO ERROR IN EMULATOR ALSO
THIS IS MY SqliteHelper.JAVA CLASS
i am able to create one table but not able to create two table there is no error is showing im using DB browser to check
package com.example.administrator.billing;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SqliteHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="billing";
public static final String TABLE_NAME_PRODUCT_MASTER ="product_master";
public static final String TABLE_NAME_SALES ="sales_master";
//product master fields
public static final String mstr_id="mstr_id";
public static final String mstr_product_code="mstr_product_code";
public static final String mstr_product_name="mstr_product_name";
public static final String mstr_cgstper="mstr_cgstper";
public static final String mstr_sgstper="mstr_sgstper";
public static final String mstr_cessper="mstr_cessper";
public static final String mstr_defrate="mstr_defrate";
// salebill
public static final String slbl_id="slbl_id";
public static final String slbl_product_code="slbl_product_code";
public static final String slbl_qty="slbl_qty";
public static final String slbl_rate="slbl_rate";
public static final String slbl_tot="slbl_tot";
public static final String slbl_cgst="slbl_cgst";
public static final String slbl_sgst="slbl_sgst";
public static final String slbl_cess="slbl_cess";
public static final String slbl_net="slbl_net";
public SqliteHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
//SQLiteDatabase db=this.getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS "+TABLE_NAME_PRODUCT_MASTER+" (mstr_id integer primary key AUTOINCREMENT,mstr_product_code integer,mstr_product_name text not null," +
" mstr_cgstper double(18,2), mstr_sgstper double(18,2),mstr_cessper double(18,2),mstr_defrate double(18,2))");
db.execSQL("CREATE TABLE IF NOT EXISTS "+TABLE_NAME_SALES+" (slbl_id INTEGER primary key AUTOINCREMENT,slbl_product_code integer,slbl_qty double(18,2)," +
" slbl_rate double(18,2), slbl_tot double(18,2),slbl_cgst double(18,2),slbl_sgst double(18,2)" +
",slbl_cess double(18,2),slbl_net double(18,2))");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE "+TABLE_NAME_PRODUCT_MASTER);
db.execSQL("DROP TABLE "+TABLE_NAME_SALES);
onCreate(db);
}
public boolean insertProductMaster(String Mstr_product_code,String Mstr_product_name, String Mstr_cgstper, String Mstr_sgstper, String Mstr_cessper,String Mstr_defrate){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(mstr_product_code,Mstr_product_code);
contentValues.put(mstr_product_name,Mstr_product_name);
contentValues.put(mstr_cgstper,Mstr_cgstper);
contentValues.put(mstr_sgstper,Mstr_sgstper);
contentValues.put(mstr_cessper,Mstr_cessper);
contentValues.put(mstr_defrate,Mstr_defrate);
long result=db.insert(TABLE_NAME_PRODUCT_MASTER,null,contentValues);
if(result == -1){
return false;
}else{
return true;
}
}
}
AND THIS IS MY MAIN ACTIVITY.JAVA
package com.example.administrator.billing;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
SqliteHelper myDb;
Button salebill;
Button product_master;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb=new SqliteHelper(this);
product_master=(Button) findViewById(R.id.prduct_master);
salebill=(Button) findViewById(R.id.salebill);
salebill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,SaleBill.class);
startActivity(i);
}
});
product_master.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent producti = new Intent(MainActivity.this, product_master.class);
startActivity(producti);
}
});
}
}
Android table creation error for multiple tables i thing on create method is went wrong somewhere

How can I create a table in SQLite?

I have prepared a code to create a database as well as table in SQLITE ANDROID STUDIO, but I am getting error in SQL statement.
Image of error is here :- ERROR IMAGE
package com.example.thakkar.registration;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.View;
public class DataBaseHelper extends SQLiteOpenHelper {
public static final String DATABASAE_NAME="student.db";
public static final String TABLE_NAME="student_table";
public static final String COL_1="NAME";
public static final String COL_2="EMAIL";
public static final String COL_3="PASSWORD";
public DataBaseHelper(Context context) {
super(context, DATABASAE_NAME, null, 1);
SQLiteDatabase db=this.getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table"+ TABLE_NAME+ "(NAME TEXT,EMAIL TEXT,PASSWORD TEXT)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXIST"+ TABLE_NAME);
onCreate(db);
}
}
You need to separate table with space before TABLE_NAME,
Something like ,
db.execSQL("create table "+ TABLE_NAME+ "(NAME TEXT,EMAIL TEXT,PASSWORD TEXT)");
Also do the same in onUpgrade() method,
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);

SQLiteDatabase.execSQL() is not working. It gives me the message "Non-static method 'execSQL' cannot be a reference from a static context"

I am trying to create a table in the Android SQLite database. But execSQLite method does not work in the on create method. It gives me the message-
Non-static method 'execSQL' cannot be a reference from a static context
But the method I am calling from is a non-static method.
The method is given below.
public void onCreate(SQLiteDatabase sqLiteDatabase) {
SQLiteDatabase.execSQL();
}
Here is my full java code:
package com.sarsinetvarneshon.basicdb;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Sarsinet Varneshon on 23-Apr-18.
*/
public class EmployeeDatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "db_employee";
public static final int VERSION = 1;
public static final String EMPLOYEE_TABLE_NAME = "tbl_employee";
public static final String EMPLOYEE_COL_ID = "id";
public static final String EMPLOYEE_COL_NAME = "name";
public static final String EMPLOYEE_COL_AGE = "age";
public static final String EMPLOYEE_COL_DEPT = "dept";
public static final String CREATE_TABLE_EMPLOYEE = " CREATE TABLE "+ EMPLOYEE_TABLE_NAME+
"("+EMPLOYEE_COL_ID+ " INTEGER PRIMARY KEY, "+
EMPLOYEE_COL_NAME+ " TEXT, "+
EMPLOYEE_COL_AGE+ " INT, "+
EMPLOYEE_COL_DEPT+ " TEXT)";
public EmployeeDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
SQLiteDatabase.execSQL();
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
Change the line:
SQLiteDatabase.execSQL();
To this line:
sqLiteDatabase.execSQL();
You are using a Class Name instead of the Variable.
You need to call sqLiteDatabase.execSQL(); not SqLiteDatabase.execSQL();
Because execSQL() method is not a static method, it needs to be called using an object from SQLiteDatabase, and it cannot be called using the class itself.

database name doesn't appear in file explorer eclipse uding sqlite android

This is my code,my database doesn't show in file explorer.does i make mistake??can anyone have a solution??
I'm sorry because im just beginner in android
package com.testmysqlite.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqliteOpenHelper extends SQLiteOpenHelper {
private static String dbName = "TestDatabase.db";
private static String tableName = "User";
private static String columnId = "Id";
private static String columnName = "Name";
private String sql = "create table "+tableName+"("+columnId+" integer,"+columnName+" text)";
private SQLiteDatabase sqliteDatabase = null;
public MySqliteOpenHelper(Context context) {
super(context, dbName, null, 1);
sqliteDatabase = getWritableDatabase();
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql);// TODO Auto-generated method stub
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
Take a look at this:link
link2

xml string from class android

This code crashes the application, If i refer to database name from the static variable it works. When i try to get it from the strings.xml it crashes the app. Any idea why it fails? This is a class not an activity so i imported android.content.res.Resources. Also if i try context.getString it will also crash.
import android.content.Context;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
//public static final String DATABASE_NAME = "library.db";
public static final String TITLE = "title";
public static final String AUTHOR = "author";
public static final String ISBN = "isbn";
public DatabaseHelper(Context context) {
super(context, Resources.getSystem().getString(R.string.DATABASE_NAME), null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE books (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, author TEXT, isbn TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
You don't need to use a string resource for your database name, and in fact it doesn't make much sense to. String resources are mainly for anything the user will see so that you can provide alternative resources for other configurations (specifically, other languages). What you are providing is essentially a file name for your database. Since this is entirely internal to your app and the user will never see it, you should just use a static final String with the name you want for your database file.
/* Database file name */
private static final String BD_NAME = "something.db";
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, null);
}
it is wrong
super(context, Resources.getSystem().getString(R.string.DATABASE_NAME), null, 1);
Try This
super(context, context.getResources().getString(R.string.DATABASE_NAME), null, 1);
change
Resources.getSystem().getString(R.string.DATABASE_NAME)
to
context.getResources().getString(R.string.DATABASE_NAME)

Categories

Resources