SQLite database's been created but not the table (android) - android

A lot of questions already have been asked about this but none of them seems to be working for me.
I open the file explorer>data>data>package name>database>mydatabase.db in order to get my database and i save it on my pc n try to see it using db browser for sqlite. But there's no table.
here's the helper class that extends sqliteOpenHelper:
package famiddle.smart.apps.businessmanager;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class BusinessDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final String DATABASE_NAME = "BusinessManager.db";
public static final int DATABASE_VERSION = 1;
// SQL query for creating the table for items name
private static final String SQL_CREATE_ITEMS_NAME_TABLE =
"CREATE TABLE itmes_name(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, items_name TEXT NOT NULL);" ;
public BusinessDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(SQL_CREATE_ITEMS_NAME_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
In my one of app's activity, I instantiate above class and run a method getReadableDatabase() so that the database can be created with the table.
package famiddle.smart.apps.businessmanager;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import androidx.appcompat.app.AppCompatActivity;
public class PurchaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_purchase);
// Instantiate the subclass of SQLiteOpenHelper.
BusinessDbHelper dbHelper = new BusinessDbHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
}
what do i do?

Everything is fine with this code. I wasn't extracting two other files with the database file and wasn't putting it into one folder. When I did put all three files into one folder and opened the file with .db extension using the db browser for SQLite, I could see the tables.

Related

Error related to Database is not created in android studio [duplicate]

This question already has answers here:
When does SQLiteOpenHelper onCreate() / onUpgrade() run?
(15 answers)
Closed 3 years ago.
I have made a database where name , email and password is saved.
When saving these details into the database, it is showing me error like - database doesn't have any column named email.
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(table Users has no column named Email (code 1): , while compiling: INSERT INTO Users(Password,Email,Name) VALUES (?,?,?))
at com.example.foody.Storage.newUser(Storage.java:42)
at com.example.foody.Register$1.onClick(Register.java:32) "missing query or database"
Storage Class(helper)
package com.example.foody;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.strictmode.SqliteObjectLeakedViolation;
public class Storage extends SQLiteOpenHelper
{
// for table 1
public static final String d_name="Foody.db";
public static final String t1="Users";
public static final String c1="Name",c2="Email",c3="Password";
public Storage(Context ct)
{
super(ct,d_name,null,1);
}
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE "+t1+"("+c1+" TEXT,"+c2+" TEXT,"+c3+" TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("Drop Table If EXISTS "+t1);
onCreate(db);
}
public void newUser(String a,String b,String c)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(c1,a);
cv.put(c2,b);
cv.put(c3,c);
db.insert(t1,null,cv);
db.close();
}
public boolean checkUser(String email,String pass)
{
SQLiteDatabase db=this.getWritableDatabase();
Cursor cr=db.rawQuery("select * from " + t1 + " where Email = ? AND Password = ? ", new String[] {email,pass});
if(cr.getCount()==0){
return false;}
else{
return true;}
}
}
```
Now this is the class from where I'm calling
```
package com.example.foody;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Register extends AppCompatActivity
{
Button b1;
EditText e1,e2,e3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
e1=findViewById(R.id.sign_e1);
e2=findViewById(R.id.sign_e2);
e3=findViewById(R.id.sign_e3);
b1=findViewById(R.id.sign_b1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Storage sob=new Storage(Register.this);
sob.newUser(e1.getText().toString(),e2.getText().toString(),e3.getText().toString());
Toast.makeText(Register.this,"You are registered",Toast.LENGTH_LONG).show();
Intent it=new Intent(Register.this,Login.class);
startActivity(it);
}
});
}
}
Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing
database.
You need to re-install your app again since you have modified some part of your database/table(s). The version now is totally different with the previous.

SQL Lite database not creating android

i have a very simple code i am trying to create SQL lite database but it's not creating while i check my DEVICE FILE EXPLORER folder there is not any database folder there under my apps folder.
here is the code:
DatabaseHelper.java
package com.example.ali.schoolapp;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Student.db";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db = this.getWritableDatabase();
db.execSQL("CREATE TABLE Items " + " (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, description TEXT)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
}
}
MainActivity.java
package com.example.ali.schoolapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
}
}
The database will not get created until you attempt to do something with it. Only then (when getWritableDatabase or getReadableDatabase is called) will the database be created.
So you could try :-
myDb = new DatabaseHelper(this); //<<<<<<<<<<< EXISTING LINE
Cursor csr = myDB.getWritableDatabase().query("Items",null,null,null,null,null,null);
csr.close();
And then the Database would have been created.
Note the above isn't how you would typically access the database, it's a quick fix. Normally you'd have you access methods in a Class (perhaps in the DatabaseHelper Class).
P.S. your onUpgrade method would very likely fail if you changed the version number (4th parameter (which is 1) when you call super). That is onUpgrade calls onCreate as the table exists you will get an error. You would typically DROP the table before calling onCreate).

Saving data to the sqlite database?

This is my main activity and I'm getting exception at 29th line of insertData() in MainActivity method and MyDatabaseAdapter() class contains the schema.Can u please help me with where I'm getting the error?
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
MyDatabaseAdapter myDatabaseAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.editText);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name=editText.getText().toString();
long id=myDatabaseAdapter.insertData(name);
if(id<0)
{
Toast.makeText(MainActivity.this,"Unsuccessful",Toast.LENGTH_LONG);
}
else
{
Toast.makeText(MainActivity.this,"Successfull",Toast.LENGTH_LONG).show();
}
}
});
}
}
This is my sqlite helper class
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class MyDatabaseAdapter {
MyDatabase myDatabase;
MyDatabaseAdapter(Context context)
{
myDatabase=new MyDatabase(context);
}
public long insertData(String name)
{
SQLiteDatabase sqLiteDatabase=myDatabase.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(MyDatabase.NAME,name);
long id=sqLiteDatabase.insert(myDatabase.TABLE_NAME,null,contentValues);
return id;
}
static class MyDatabase extends SQLiteOpenHelper
{
private static final String DATABASE_NAME="Mydatabase";
private static final int DATABASE_VERSION=3;
private static final String TABLE_NAME="MyContacts";
private static final String NAME="Name";
// private static final String ADDRESS="Address";
private static final String UID="_id";
private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME+" VARCHAR(255),;";
private static final String DROP_TABLE="DROP TABLE IF EXISTS" +TABLE_NAME;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Toast.makeText(context,"Constructor is called",Toast.LENGTH_LONG).show();
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE);
onCreate(db);
}
}
}
you did not instantiate your MyDatabaseAdapter class. Try
myDatabaseAdapter = new MyDatabaseAdapter(this) after setContentView().
Firstly, you should really be able to give us more information. Your logcat output should show exactly what the error is or run with the debugger to catch the exception as it happens. Anyhow, with the info provided, at least one problem is that your create statement has invalid syntax. It evaluates as:
CREATE TABLE MyContacts(_id INTEGER PRIMARY KEY AUTOINCREMENT,Name VARCHAR(255),;
The comma and semi-colon are not required and there is no closing bracket. It should be:
CREATE TABLE MyContacts(_id INTEGER PRIMARY KEY AUTOINCREMENT,Name VARCHAR(255))
Or more correctly:
CREATE TABLE MyContacts(_id INTEGER PRIMARY KEY AUTOINCREMENT,Name TEXT)
...because there is no VARCHAR type in SQLite. Type affinity converts your VARCHAR(255) to TEXT because it contains the string 'CHAR' but it is best to specify SQLite types only to ensure you get what you expect.
Sorry if I'm talking something wrong, I'm still learning.
But in line 29, considering that you have 2 lines for thepackage and one break, this is the line:
shouldn't long be used for numbers ?
long id=myDatabaseAdapter.insertData(name);
"long: The long data type is a 64-bit two's complement integer. The
signed long has a minimum value of -263 and a maximum value of 263-1.
In Java SE 8 and later, you can use the long data type to represent an
unsigned 64-bit long, which has a minimum value of 0 and a maximum
value of 264-1. Use this data type when you need a range of values
wider than those provided by int. The Long class also contains methods
like compareUnsigned, divideUnsigned etc to support arithmetic
operations for unsigned long."

First database creation in android

I have an app and I didn't use database up the present.Now I will update my app and this app contains a database.I am curious about this.Which method will call on updated phones in database onCreate or onUpgrade ?
first time if db don't exist, onCreate() method will run.
if db exist and your current db version is higher than installed version, onUpgrade() will run and you should check version in onUpgrade method and do what you want.
here is an example of dbHandler.java class.
package com.app.util;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Milad
*/
public class DBHandler extends SQLiteOpenHelper {
private static final String DB_NAME = "YourDbName";
private static final int DB_VERSION = 1;
public DBHandler(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
//Create firstTable
db.execSQL("CREATE TABLE TEST ( _id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion >2) {
db.execSQL("DROP TABLE IF EXISTS TEST");
onCreate(db);
}
}
}
When you first create the database onCreate() method will be called. If you upgrade the database version then that time onUpgrade() method will be called.

press the save button EditText value is stored in to the database

I have four EditText and two buttons namely, save and review button.
If i press the save button EditText value is stored in to the database and on clicking the review button saved value in the database is listed in ListView.
How is this done please anybody explain to me.
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, "MedicinePill", null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS names3 ("
+ BaseColumns._ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, first VARCHAR, last VARCHAR ,dose2 VARCHAR ,dose3 VARCHAR)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Steps to upgrade the database for the new version ...
}
}
//this is my database class in this where i add the insert data from the database
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Medical extends Activity
{
Button AddMedicine,ReviewMedicine;
EditText dose1,dose2,dose3;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AddMedicine = (Button)findViewById(R.id.add);
ReviewMedicine = (Button)findViewById(R.id.rev);
dose1 = (EditText)findViewById(R.id.Edit1);
AddMedicine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
String s1 = dose1.getText().toString();
// TODO Auto-generated method stub
}
});
}
}
//and this is my activity class
how i add edittext value in to database pls any body help me!!!!!!!!
You need to create class insertdose in DatabaseHelper like this:
public long insertdose(String dose1)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_dose, dose1);
return db.insert(DATABASE_TABLE, null, initialValues);
}
and call it at your onClick view
You will need to create a method in the DatabaseHelper class that takes in four strings and adds them to your database.
Then in your onClick() method in your Medical activity class you can get the string values from your 4 EditText fields and call your method with these values.
I would recommend going through the Notepad Tutorial. This should provide a good example of what you want to do.

Categories

Resources