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.
Related
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.
I'm new at the android studio. I tried creating SQLite Database but when I try inserting data android studio cannot resolve it. i also tried MainActivity.this and getApplicationContext instead of db=new DatabaseHelper(this); on this line. and on device monitor, it isn't creating a database file
MainActivity.java
package com.example.lenovo.sqlite_2_deneme;
import android.content.Context;
import android.media.midi.MidiDevice;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
DatabaseHelper db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=new DatabaseHelper(this);
EditText et_name,et_surname,et_number;
Button btn_data;
et_name= (EditText) findViewById(R.id.et_isim);
et_surname= (EditText) findViewById(R.id.et_soyisim);
et_number= (EditText) findViewById(R.id.et_numara);
btn_data= (Button) findViewById(R.id.btn_veri);
btn_data.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
db.insertData();
}
});
}
}
and my DatabaseHelper class
package com.example.lenovo.sqlite_2_deneme;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.ContactsContract;
import android.widget.TableLayout;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME= "student_info.db";
public static final String TABLE_NAME="student_table";
public static final String COL_1="ID";
public static final String COL_2="NAME";
public static final String COL_3="SURNAME";
public static final String COL_4="NUMBER";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE"+ TABLE_NAME+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, NUMBER INTEGER)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME);
this.onCreate(db);
}
public Boolean DataInsert(String name, String surname, String number)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,number);
long sonuc= db.insert(TABLE_NAME,null,contentValues);
if(sonuc==-1)
return false;
else
return true;
}
}
Your database will be created at the instance when you call
SQLiteDatabase db=this.getWritableDatabase();
At the moment I dont see you have called this anywhere in your code. You have written a DataInsert method but it is not getting called in the code you have shared so far.
your'e sql query is wrong give the space in table (insert data),and also in onupgrade and there is no method insertData()
db.execSQL("CREATE TABLE "+ TABLE_NAME+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, NUMBER INTEGER)");
There is no method insertData() . You should use
DataInsert() method .
db=new DatabaseHelper(MainActivity.this);
EditText et_name,et_surname,et_number;
Button btn_data;
et_name= (EditText) findViewById(R.id.et_isim);
et_surname= (EditText) findViewById(R.id.et_soyisim);
et_number= (EditText) findViewById(R.id.et_numara);
btn_data= (Button) findViewById(R.id.btn_veri);
btn_data.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
db.DataInsert(et_name.getText().toString(),et_surname.getText().toString(),et_number.getText().toString());
}
});
FYI
void onCreate (SQLiteDatabase db)
Called when the database is created for the first time.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 6 years ago.
I'm trying to create a basic database and insert three users, but it doesn't work.
This is a class that creates the database
package com.example.matadoor.db1;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by matadoor on 06/08/2016.
*/
public class dbcreat extends SQLiteOpenHelper {
public final String tbname="t1";
public final String cid="id";
public static String cid2="test";
public final String user="username";
public final String creat="CREATE TABLE "+cid2+"("+cid+"INTEGER primery key AUTO_INCREMENT , "+user+" TEXT); ";
public dbcreat(Context context) {
super(context,cid2,null, 1);
}
public dbcreat(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(creat);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
And it's the other class which handles the database - opens it, closes it, and inserts into it
package com.example.matadoor.db1;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
* Created by matadoor on 08/08/2016.
*/
public class dbhandler {
private dbcreat dbc;
private SQLiteDatabase sdb;
public dbhandler(Context context)
{
dbc=new dbcreat(context);
}
public void open()
{
sdb=dbc.getWritableDatabase();
}
public void close(){
dbc.close();
}
public String display(int row)
{
Cursor cu=sdb.query(dbc.tbname,null,null,null,null,null,null);
cu.moveToPosition(row);
String user=cu.getString(1);
return user;
}
public void insert(String user)
{
ContentValues cn=new ContentValues();
cn.put(dbc.user,user);
}
}
And the main class which has one Button for inserting a user into the db
package com.example.matadoor.db1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main extends AppCompatActivity {
Button btn;
dbhandler db;
dbcreat dbc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.ins);
// dbc.getWritableDatabase();
db.open();
db=new dbhandler(this);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String [] user={"ali","reza","ahmed"};
for(int i=0;i<user.length;i++)
{
db.insert(user[i]);
}
}
});
}
}
So this seems to be a error. You need to switch those two lines.
db.open(); // NullPointer here
db=new dbhandler(this);
Assuming there are no other errors with the code, this method is doing nothing to the database.
public void insert(String user)
{
ContentValues cn=new ContentValues();
cn.put(dbc.user,user);
}
You have to call sdb.insert with that cn variable.
I am new to Android Programming.. I am trying to create sqLite Database in android Here is my code
sqLitee.java
package com.example.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class sqLitee extends SQLiteOpenHelper
{
public static final String DB_NAME="student.db";
public static final String TABLE_NAME="student";
public static final String COL_1="ID";
public static final String COL_2="FIRST_NAME";
public static final String COL_3="LAST_NAME";
public static final String COL_4="MARKS";
public sqLitee(Context context)
{
super(context, DB_NAME,null, 1);
// TODO Auto-generated constructor stub
SQLiteDatabase db=this.getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTO INCREMENT, FIRST_NAME TEXT, LAST_NAME TEXT, MARKS INTEGER)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXIST " + TABLE_NAME);
onCreate(db);
}
}
MainActivity.java
package com.example.sqlite;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
sqLitee myDB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myDB = new sqLitee(this);
setContentView(R.layout.activity_main);
}
}
When I am trying to run it on Android virtual Device , It says unfortunately , sqLite has stopped..
Thanks
remove this line in your sqllite constructor:
SQLiteDatabase db=this.getWritableDatabase();
you try to get the db, which is currently initializing.
There's a syntax error in your CREATE TABLE SQL. AUTO INCREMENT should be AUTOINCREMENT.
As always, first see the logcat for exception stacktrace and to learn what is failing and where.
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.