I don't know what is the problem of my database, my app keep stopping after i added the data base code.
Please can you help with to fix that.
I have tried many times to figure it out but still not getting anywhere.
This is my data base code :
public class databaseOpenHelper extends SQLiteOpenHelper{
// Country table name
private static final String TABLE_NAME= "contacts";
// Country Table Columns names
private static final String KEY_ID = "id";
private static final String NAME = "Name";
private static final String PHONENO = "PhoneNo";
public databaseOpenHelper(Context context){
super(context,"Login.db",null,1);
}
#Override
public void onCreate(SQLiteDatabase myDB) {
myDB.execSQL("create Table users(username Text primary key,password Text)");
// create the table for the first time
String CREATE_COUNTRY_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + NAME + " TEXT,"
+ PHONENO + " TEXT" + ")";
myDB.execSQL(CREATE_COUNTRY_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase myDB, int i, int i1) {
myDB.execSQL("drop Table if exists users");
}
public Boolean isertData(String username,String password){
SQLiteDatabase myDB = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("username",username);
contentValues.put("password",password);
long result = myDB.insert("users",null,contentValues);
if(result == -1){
return false;
}
else {
return true;
}
}
public Boolean checkusername(String username){
SQLiteDatabase myDB = this.getWritableDatabase();
Cursor cursor = myDB.rawQuery("select * from users where username = ?",new String[] {username});
if (cursor.getCount()>0){
return true;
}
else {
return false;
}
}
public Boolean checkusernamePassword(String username,String password){
SQLiteDatabase myDB = this.getWritableDatabase();
Cursor cursor = myDB.rawQuery("select * from users where username = ? and password = ?",new String[] {username,password});
if (cursor.getCount()>0){
return true;
}
else{
return false;
}
}
// method to add the contact
public void addcontact(ContactModel contact){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues c=new ContentValues();
c.put(NAME,contact.getName());
c.put(PHONENO,contact.getPhoneNo());
db.insert(TABLE_NAME,null,c);
db.close();
}
// method to retrieve all the contacts in List
public List<ContactModel> getAllContacts(){
List<ContactModel> list=new ArrayList<>();
String query="SELECT * FROM "+TABLE_NAME;
SQLiteDatabase db=this.getReadableDatabase();
Cursor c=db.rawQuery(query,null);
if(c.moveToFirst()) {
do {
list.add(new ContactModel(c.getInt(0),c.getString(1),c.getString(2)));
} while (c.moveToNext());
}
return list;
}
// get the count of data, this will allow user
// to not add more that five contacts in database
public int count(){
int count=0;
String query="SELECT COUNT(*) FROM "+TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor c=db.rawQuery(query,null);
if(c.getCount()>0){
c.moveToFirst();
count=c.getInt(0);
}
c.close();
return count;
}
// Deleting single country
public void deleteContact(ContactModel contact) {
SQLiteDatabase db = this.getWritableDatabase();
int i=db.delete(TABLE_NAME,KEY_ID + " = ?",
new String[] { String.valueOf(contact.getId()) });
db.close();
}
}
And this is my logcat :
FATAL EXCEPTION: main
Process: com.example.localisation, PID: 11700
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.localisation/com.example.localisation.phone}: android.database.sqlite.SQLiteException: no such table: contacts (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM contacts
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: android.database.sqlite.SQLiteException: no such table: contacts (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM contacts
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1047)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:654)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:62)
at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1546)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1485)
at com.example.localisation.databaseOpenHelper.getAllContacts(databaseOpenHelper.java:108)
at com.example.localisation.phone.onCreate(phone.java:81)
at android.app.Activity.performCreate(Activity.java:8051)
at android.app.Activity.performCreate(Activity.java:8031)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
The failure is saying that there is not a table called contacts as per
Caused by: android.database.sqlite.SQLiteException: no such table: contacts (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM contacts
Running your code and then using the errant getAllContacts method, as indicated in the log by:-
at com.example.localisation.databaseOpenHelper.getAllContacts(databaseOpenHelper.java:108)
Works.
As such it is highly likely that you have added the contacts cable after previously running the App and that this has created the database. The database persists and thus will still exist from on run to the other. As such the onCreate method will not be called, as it is only called once when the database is created.
The easy fix is to uninstall the App and rerun. However, this will delete any existing data.
If you need to keep any data then you should a) increase the version number and then b) add code to the onUpgrade method to create the new table (same code).
For example:-
a)
public databaseOpenHelper(Context context){
super(context,"Login.db",null,2 /*<<<<<<<<<< CHANGED */);
}
b)
#Override
public void onUpgrade(SQLiteDatabase myDB, int i, int i1) {
//myDB.execSQL("drop Table if exists users");
if (i1 == 2) {
String CREATE_COUNTRY_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + NAME + " TEXT,"
+ PHONENO + " TEXT" + ")";
myDB.execSQL(CREATE_COUNTRY_TABLE);
}
}
You can the see that the contacts table has been created by using App Inspection e.g.
according to the error code you uploaded i noticed this SELECT * FROM contacts which means that you don't have table named contacts in you sqlite database so i recommend you, for testing you should use sqlite browser to create database and add data to it and then use it in your android studio project. if you get any problem fearther don't hesitate to ask it in comments thanks.
Related
I am a beginner in android development. As per my testing, I found that the app is not running because of following line in MainActivity.java.
tasklist=db.getAllTasks();
here is the main Activity code
<pre><code>
public class MainActivity extends AppCompatActivity implements DialogEndListener{
private ToDoAdapter taskAdapter;
private List<ToDoModel> tasklist;
private Databases db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Objects.requireNonNull(getSupportActionBar()).hide();
RecyclerView taskRecyclerView = findViewById(R.id.taskRecler);
taskRecyclerView.setLayoutManager(new LinearLayoutManager(this));
taskAdapter=new ToDoAdapter(db,this);
taskRecyclerView.setAdapter(taskAdapter);
tasklist =new ArrayList<>();
FloatingActionButton fab = findViewById(R.id.floatingActionButton);
db=new Databases(this);
db.openDatabse();
ItemTouchHelper itemTouchHelper=new ItemTouchHelper(new ListTouchHandler(taskAdapter));
itemTouchHelper.attachToRecyclerView(taskRecyclerView);
tasklist=db.getAllTasks();
Collections.reverse(tasklist);
taskAdapter.setTasks(tasklist);
fab.setOnClickListener(v -> NewTaskAddition.newInstance().show(getSupportFragmentManager(),NewTaskAddition.TAG));
}
#SuppressLint("NotifyDataSetChanged")
#Override
public void handleDialogClose(DialogInterface dialogInterface) {
tasklist=db.getAllTasks();
Collections.reverse(tasklist);
taskAdapter.setTasks(tasklist);
taskAdapter.notifyDataSetChanged();
}
}
</pre></code>
I think the fault must be in database code.
<pre><code>
public class Databases extends SQLiteOpenHelper {
private static final int VESRSION =1;
private static final String NAME="appDatabase";
private static final String TODO_TABLE="todoTable";
private static final String ID="todoId";
private static final String TASK="taskDesc";
private static final String STATUS="taskStatus";
private static final String CREATE_TODO_TABLE="CREATE TABLE "+ TODO_TABLE+" ( "+ID+
" INTEGER PRIMARY KEY AUTOINCREMENT, "
+ TASK +"TEXT, "+STATUS+
" INTEGER)";
private SQLiteDatabase db;
public Databases(#Nullable Context context) {
super(context, NAME, null, VESRSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TODO_TABLE); //embedded sql
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//drop existing table
db.execSQL("DROP TABLE IF EXISTS "+ TODO_TABLE);
onCreate(db);
}
public void openDatabse(){
db=this.getWritableDatabase();
}
public void insertTasks (ToDoModel tasks){
ContentValues value= new ContentValues();
value.put(TASK, tasks.getEachtask());
value.put(STATUS,0);
db.insert(TODO_TABLE,null, value);
}
public List<ToDoModel> getAllTasks(){
List<ToDoModel> taskList =new ArrayList<>();
db.beginTransaction();
try (Cursor sqlcursor = db.query(String.valueOf(Boolean.parseBoolean(TODO_TABLE)), null, null,
null, null, null,
null, null)) {
if (sqlcursor != null) {
if (sqlcursor.moveToFirst()) {
do {
ToDoModel thisTask = new ToDoModel();
thisTask.setId(sqlcursor.getInt(sqlcursor.getColumnIndex(ID)));
thisTask.setEachtask(sqlcursor.getString(sqlcursor.getColumnIndex(TASK)));
thisTask.setStatus(sqlcursor.getInt(sqlcursor.getColumnIndex(STATUS)));
taskList.add(thisTask);
} while (sqlcursor.moveToNext());
}
}
} finally {
db.endTransaction();
}
return taskList;
}
public void updateStatus(int id,int status){
ContentValues value=new ContentValues();
value.put(STATUS,status);
db.update(TODO_TABLE,value,ID+"=?",new String[]{String.valueOf(id)});
}
public void updateTask(int id,String tasks){
ContentValues value=new ContentValues();
value.put(TASK,tasks);
db.update(TODO_TABLE,value,ID+"=?",new String[]{String.valueOf(id)});
}
public void deleteTask(int id){
db.delete(TODO_TABLE,ID+"=?",new String[]{String.valueOf(id)});
}
}
</pre></code>
Can someone help me identifying the error. Thanks in advance.
You are trying to access a table called false or true as you are using String.valueOf(Boolean.parseBoolean(TODO_TABLE)) for the table name (neither table exists).
If you inspected the log then you would have seen something along the lines of :-
2021-10-09 10:49:14.468 25593-25593/a.a.so69499435manager E/SQLiteLog: (1) no such table: false
2021-10-09 10:49:14.468 25593-25593/a.a.so69499435manager D/AndroidRuntime: Shutting down VM
2021-10-09 10:49:14.471 25593-25593/a.a.so69499435manager E/AndroidRuntime: FATAL EXCEPTION: main
Process: a.a.so69499435manager, PID: 25593
java.lang.RuntimeException: Unable to start activity ComponentInfo{a.a.so69499435manager/a.a.so69499435manager.MainActivity}: android.database.sqlite.SQLiteException: no such table: false (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM false
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.database.sqlite.SQLiteException: no such table: false (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM false
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1408)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1255)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1126)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1332)
at a.a.so69499435manager.Databases.getAllTasks(Databases.java:58)
i.e. the table false could not be found because you query ends up being :-
SELECT * FROM false
You may wish to have a read of https://developer.android.com/studio/debug/#systemLog (and other sections in the linked page)
I believe you should just be using :-
Cursor sqlcursor = db.query(TODO_TABLE, null, null,
null, null, null,
null, null)
However :-
Running a single SQL in a transaction has no benefit.
A Cursor will NEVER be null when returned from any SQliteDatabase method that returns a Cursor, Checking for null is useless.
Try/catch on SQLite more often than not introduces confusion. If an exception occurs then more than likely it MUST be fixed. e.g. you appear to have missed the table false not found error
while(thecursor.moveToNext) { .... } is more concise then a moveToFirst while moveToNext loop.
you should ALWAYS close a cursor when done with it (not doing so may eventually result in a crash because too many Cursors are open).
As such perhaps consider the following :-
public List<ToDoModel> getAllTasks(){
List<ToDoModel> taskList =new ArrayList<>();
Cursor sqlcursor = db.query(TODO_TABLE, null, null,
null,null,null,null,null);
while(sqlcursor.moveToNext()) {
ToDoModel thisTask = new ToDoModel();
thisTask.setId(sqlcursor.getInt(sqlcursor.getColumnIndex(ID)));
thisTask.setEachtask(sqlcursor.getString(sqlcursor.getColumnIndex(TASK)));
thisTask.setStatus(sqlcursor.getInt(sqlcursor.getColumnIndex(STATUS)));
taskList.add(thisTask);
}
sqlcursor.close();
return taskList;
}
More
You may also wish to address the fact that the table will not have a column named taskDesc but will instead have a column named taskDescTEXT as you have omitted a space between the column name and it's type.
Instead of
private static final String CREATE_TODO_TABLE="CREATE TABLE "+ TODO_TABLE+" ( "+ID+
" INTEGER PRIMARY KEY AUTOINCREMENT, "
+ TASK +"TEXT, "+STATUS+
" INTEGER)";
results in
You probably want :-
private static final String CREATE_TODO_TABLE="CREATE TABLE "+ TODO_TABLE+" ( "+ID+
" INTEGER PRIMARY KEY AUTOINCREMENT, "
+ TASK +" TEXT, "+STATUS+ //<<<<<<<<<< ADDED a space before TEXT
" INTEGER)";
results in
If you make this change (add the space). The easy way to do this is to uninstall the App and then rerun it after changing the code. However, you will lose any existing data.
If you need to retain data and apply the change and don't know how then I'd suggest asking another question.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am stuck at how do I avoid duplicate rows in Sqlite Database.I tried every possible answer on StackOverflow but none of them worked for me.I tried using unique constraint, db.insertWithOnConflict() but both of them was of no use.
Can anyone please help me to figure out my mistake?
Here is code from my Android studio:
Params.java:
public class Params {
public static final int DB_VERSION=1;
public static final String DB_NAME="PDF_NAME";
public static final String TABLE_NAME="PDF_TABLE";
public static final String KEY_ID="ID";
public static final String KEY_NAME="NAME";
public static final String KEY_PAGE="PAGE";
}
MyDbHandler.java
public class MyDbHandler extends SQLiteOpenHelper {
public static SQLiteDatabase database;
public MyDbHandler(Context context) {
super(context, Params.DB_NAME, null, Params.DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
String create="CREATE TABLE "+Params.TABLE_NAME+"("+Params.KEY_ID+" INTEGER PRIMARY KEY,"+Params.KEY_NAME+" TEXT UNIQUE, "
+Params.KEY_PAGE+" INTEGER"+")";
db.execSQL(create);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void addPDF(File file)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(Params.KEY_NAME,file.getName());
values.put(Params.KEY_PAGE, ViewPdf.pagenumber);
db.insertWithOnConflict(Params.TABLE_NAME,null,values,SQLiteDatabase.CONFLICT_REPLACE);
Log.d("pa","pagenumber"+file.getName() + ViewPdf.pagenumber);
Log.d("data","Inserted");
db.close();
}
public Cursor getALlfile()
{
database=this.getReadableDatabase();
String select="SELECT * FROM "+Params.TABLE_NAME;
Cursor cursor=database.rawQuery(select,null);
return cursor;
}
}
DocumentsFragment.java:
public void onItemClick(int position) {
Intent intent = new Intent(getContext(), ViewPdf.class);
intent.putExtra("Position", position);
startActivity(intent);
if (!HistoryFragment.pdfHistory.contains(pdf.get(position))) {
HistoryFragment.pdfHistory.add(0, pdf.get(position));
} else {
File newpdf = pdf.get(position);
int pos = HistoryFragment.pdfHistory.indexOf(newpdf);
HistoryFragment.pdfHistory.remove(pos);
HistoryFragment.pdfHistory.add(0, newpdf);
}
Cursor cursor=MyDbHandler.database.rawQuery("Select * from" + Params.TABLE_NAME + "where"+Params.KEY_NAME+ "=" +(pdf.get(position).getName()) ,null);
if(cursor.moveToFirst())
Toast.makeText(getContext(),"already exist", Toast.LENGTH_SHORT).show();
else{
db.addPDF(pdf.get(position));
}
I am attaching my logcat too if its of any use:
UPDATE
2021-05-08 17:54:15.181 11873-11873/com.flashxpdfreader.flash2021 E/SQLiteLog: (1) no such column: ghty.pdf
2021-05-08 17:54:15.182 11873-11873/com.flashxpdfreader.flash2021 D/AndroidRuntime: Shutting down VM
2021-05-08 17:54:15.183 11873-11873/com.flashxpdfreader.flash2021 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.flashxpdfreader.flash2021, PID: 11873
android.database.sqlite.SQLiteException: no such column: ghty.pdf (code 1 SQLITE_ERROR): , while compiling: Select * from PDF_TABLE where NAME = ghty.pdf
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:986)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:593)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1443)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1382)
at com.flashxpdfreader.flash2021.Fragment.DocumentsFragment.onItemClick(DocumentsFragment.java:156)
at com.flashxpdfreader.flash2021.RecylerAdapter$ViewHolder.lambda$new$0$RecylerAdapter$ViewHolder(RecylerAdapter.java:137)
at com.flashxpdfreader.flash2021.-$$Lambda$RecylerAdapter$ViewHolder$8a1BheGsPwqyn4c_IFEij433NiM.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:7185)
at android.view.View.performClickInternal(View.java:7162)
at android.view.View.access$3500(View.java:819)
at android.view.View$PerformClick.run(View.java:27684)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7562)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Here is screenshot from my Sqlite database Browser:
You can notice that I actually have that same file name in my Database but it still says no such column.
android.database.sqlite.SQLiteException: near "fromPDF_TABLEwhereNAME": syntax error (code 1 SQLITE_ERROR): , while compiling: Select * fromPDF_TABLEwhereNAME=fhg.pdf
Currently, your sql statement is like this:
Select * fromPDF_TABLEwhereNAME=fhg.pdf
You need to add space. Your sql statement need to be like this:
Select * from PDF_TABLE where NAME = fhg.pdf
In DocumentsFragment.java, try changing the code from
Cursor cursor=MyDbHandler.database.rawQuery("Select * from" + Params.TABLE_NAME + "where"+Params.KEY_NAME+ "=" +(pdf.get(position).getName()) ,null);
To
Cursor cursor=MyDbHandler.database.rawQuery("Select * from " + Params.TABLE_NAME + " where "+Params.KEY_NAME+ " = " +(pdf.get(position).getName()) ,null);
Rather check if your cursor returns something. if the itemcount is != 0 the the pdf exists.
Cursor cursor=MyDbHandler.database.rawQuery("Select * from " + Params.TABLE_NAME + " where "+Params.KEY_NAME+ "=" +(pdf.get(position).getName()) ,null);
if(cursor.getCount() != 0)
Toast.makeText(getContext(),"already exist",Toast.LENGTH_SHORT).show();
else{
db.addPDF(pdf.get(position));
}
I am trying to save data into a database, but it seems that the onCreate method is not run. Might be this might be another problem but the logcat
12-01 01:22:41.785 19724-19724/com.example.user.timetable_test E/SQLiteLog: (1) near "null": syntax error
12-01 01:22:41.795 19724-19724/com.example.user.timetable_test E/SQLiteDatabase: Error inserting _length=1 _name=Break
android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO null(_length,_name) VALUES (?,?)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(near "null": syntax error (code 1): , while compiling: INSERT INTO null(_length,_name) VALUES (?,?))
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1058)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1607)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1479)
at com.example.user.timetable_test.setup.TableDBHandler.addEntry(TableDBHandler.java:79)
at com.example.user.timetable_test.setup.SetTable$PlaceholderFragment$1.onClick(SetTable.java:227)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10826)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
The SQLite class is:
package com.example.user.timetable_test.setup;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.example.user.timetable_test.MiscData;
public class TableDBHandler extends SQLiteOpenHelper{
private MiscData data = MiscData.getInstance();
private static int DATABASE_VERSION = 1;
private static String DATABASE_NAME = "Timetable.db";
private static String[] TABLE_DAY = new String[MiscData.getInstance().getDays()];
private static String COLUMN_SESSION_NUM = "_id";
private static String COLUMN_NAME = "_name";
private static String COLUMN_LENGTH = "_length";
private SQLiteDatabase db;
public TableDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version){
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
db = getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase){
Log.i("SQL", "DB Created");
for(int i = 0; i < data.getDays(); i++){
TABLE_DAY[i] = data.getDay(i + data.getFirstDay());
}
for(int i = 0; i < data.getDays(); i++){
String query = "CREATE TABLE " + TABLE_DAY[i] + "(" + COLUMN_SESSION_NUM +
" INTEGER PRIMARY KEY ," + COLUMN_NAME + " TEXT," +
COLUMN_LENGTH + " INTEGER " + ");";
sqLiteDatabase.execSQL(query);
}
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1){
Log.i("SQL", "DB updated");
for(int j = 0; j < data.getDays(); j++){
TABLE_DAY[j] = data.getDay(j + data.getFirstDay());
sqLiteDatabase.execSQL("DROP TABLE EXISTS " + TABLE_DAY[j]);
}
onCreate(sqLiteDatabase);
}
public void addEntry(int day, String name, int length){
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_LENGTH, length);
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_DAY[day], null, values);
db.close();
}
}
I am using an array to create a dynamic number of tables.
The log message in the onCreate method doesn't show up in the logcat, I checked a lot.
Calling the addEntry method:
tableDBHandler.addEntry(page, sessionSpinners[i].getSelectedItem().toString(), Integer.parseInt(lengthText[i].getText().toString()));
If I changed the DATABASE_VERSION from 1 the onUpgrade method is called, but if the version was 1 I cannot see the message from onCreat, in the logcat.
Shouldn't this class create a .db file? Because after clicking the button, and running the method, I can't see any files in the app's folder.
INSERT INTO null - the table name is null. It comes from the TABLE_DAY array you initialize in your SQLite helper onCreate(). But onCreate() is only invoked when the database file is created, not each time you open your database.
You could move the TABLE_DAY init from onCreate() to e.g. constructor.
I am using an array to create a dynamic number of tables.
If the TABLE_DAY array is really dynamic, you're in much more trouble. Consider redesigning your database schema so that the tables are static but the content can be dynamic.
This question already has answers here:
Android Cursor Index out of Bound Exception
(3 answers)
Closed 6 years ago.
I'm trying to retrieve data from sqlite data base but but when I call the nameData() logcat shows the exception:
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
I don't understand why, any clues?
process:
public class SearchContactByName2 extends AppCompatActivity {
String dbString="",dbString2="";
SQLiteDatabase db;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.searchcontactbynamelayout2_main);
TextView textView=(TextView)findViewById(R.id.textViewShowName);
TextView textView2=(TextView)findViewById(R.id.textView2ShowNumber);
SearchContactByName objOfSearchContactByName=new SearchContactByName();
ContactDatabase onbOfContactDatabase=new ContactDatabase(getBaseContext());
Cursor allcontact2= onbOfContactDatabase.nameData(objOfSearchContactByName.getNameForSearchTypeString);
allcontact2.moveToFirst();
do{
dbString+=allcontact2.getString(allcontact2.getColumnIndex("name"));
dbString2+=allcontact2.getString(allcontact2.getColumnIndex("phone"));
dbString+="\n";
dbString2+="\n";
textView.setText(dbString);
textView2.setText(dbString2);
}while(allcontact2.moveToNext());
Toast.makeText(getBaseContext(), "data", Toast.LENGTH_LONG).show();
}
}
database part:
public class ContactDatabase extends SQLiteOpenHelper {
SQLiteDatabase db;
public static final String DATABASE_NAME="totalContact.db";
public static final String TABLE_NAME="mecontact";
public static final String NAME="name";
public static final String PHONE="phone";
public ContactDatabase(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL("create table mecontact" +
"(id integer primary key autoincrement, name text, phone text)");
}catch(android.database.SQLException e){
System.out.println("table create nhi ho rha");
}
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS mecontact");
onCreate(db);
}
public void insertContact(String nam,String mob){
db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(NAME,nam);
contentValues.put(PHONE,mob);
db.insert(TABLE_NAME, null, contentValues);
db.close();
}
public Cursor showData(){
db=this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM mecontact", null);
return res;
}
public Cursor nameData(String dataName){
db=this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM mecontact WHERE name = '"+dataName+"'", null);
return res;
}
}
try below code
if(allcontact2.moveToFirst()){
do{
dbString+=allcontact2.getString(allcontact2.getColumnIndex("name"));
dbString2+=allcontact2.getString(allcontact2.getColumnIndex("phone"));
dbString+="\n";
dbString2+="\n";
textView.setText(dbString);
textView2.setText(dbString2);
}while(allcontact2.moveToNext());}
Toast.makeText(getBaseContext(), " no data", Toast.LENGTH_LONG).show();
actually your database has no data
Cursor allcontact2= onbOfContactDatabase.nameData(objOfSearchContactByName.getNameForSearchTypeString);
if(allcontact2.size() > 0){
while(allcontact2.moveToNext()){
dbString+=allcontact2.getString(allcontact2.getColumnIndex("name"));
dbString2+=allcontact2.getString(allcontact2.getColumnIndex("phone"));
dbString+="\n";
dbString2+="\n";
textView.setText(dbString);
textView2.setText(dbString2);
}
Toast.makeText(getBaseContext(), "data", Toast.LENGTH_LONG).show();
}
Along with the answer by sush change nameData method to,
public Cursor nameData(String dataName){
db=this.getReadableDatabase();
//String dataname might contain special characters like a quote
//retreive the cursor this way
Cursor res=db.query("mecontact",new String[]{columnsYouwantToSelect},"name =?",new String[]{dataName},null,null,null);
//if you want to select all the columns in the table replace
//the second parameter with null
return res;
}
Not really answering your question, but more advice on how to prevent trouble in the future. I would suggest you change your oncreate code to this and declare your ID value in the top like you did for the others. This will make sure the database is created correctly and that in the future if changes happen you can easily get values without making typing errors. Code like this is safer to use than pure queries.
db.execSQL("CREATE TABLE "
+ TABLE_NAME
+ " (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ NAME + " TEXT,"
+ PHONE + " TEXT);" );
i'm wrote simple DataBaseHelper to use SQlite in android. after create class as :
public class DatabaseHandler extends SQLiteOpenHelper{
private static String DB_PATH = "";
private static final String DATABASE_NAME = "tsms";
private static String RECEIVE_FIELDS_TABLE = "ReceiveFields";
private static final String COLUMN_ID = "id";
private static final String COLUMN_LASTID = "lastId";
private static final String COLUMN_SMSNUMBER = "smsNumber";
private static final String COLUMN_MOBILENUMBER = "mobileNumber";
private static final String COLUMN_SENDERNAME = "senderName";
private static final String COLUMN_SMSBODY = "smsBody";
private static final String COLUMN_RECEIVEDATE = "receiveDate";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase mDataBase;
/* UPDATE DATABASE_CREATE FIELD*/
private static final String DATABASE_CREATE = "CREATE TABLE " + RECEIVE_FIELDS_TABLE + "("
+ COLUMN_ID + " INTEGER UNIQUE , "
+ COLUMN_LASTID + " INTEGER UNIQUE , "
+ COLUMN_SMSNUMBER + " VARCHAR UNIQUE , "
+ COLUMN_MOBILENUMBER + " VARCHAR , "
+ COLUMN_SENDERNAME + " VARCHAR , "
+ COLUMN_SMSBODY + " TEXT , "
+ COLUMN_RECEIVEDATE + " VARCHAR , PRIMARY KEY (" + COLUMN_ID + ", " + COLUMN_LASTID + ", " + "))";
private Context context;
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(DATABASE_CREATE);
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + RECEIVE_FIELDS_TABLE);
// Create tables again
onCreate(sqLiteDatabase);
}
// Adding new fields
public void addToReceived(ReceiveFields fields) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_LASTID, fields.getLastId()); // ReceiveFields last ID
values.put(COLUMN_MOBILENUMBER, fields.getMobileNumber()); // ReceiveFields Mobile Number
values.put(COLUMN_SENDERNAME, fields.getSenderName()); // ReceiveFields Mobile Number
values.put(COLUMN_SMSBODY, fields.getSmsBody()); // ReceiveFields Mobile Number
values.put(COLUMN_SMSNUMBER, fields.getSmsNumber()); // ReceiveFields Mobile Number
values.put(COLUMN_MOBILENUMBER, fields.getMobileNumber()); // ReceiveFields Mobile Number
values.put(COLUMN_RECEIVEDATE, String.valueOf(fields.getReceiveDate())); // ReceiveFields Mobile Number
// Inserting Row
db.insert(RECEIVE_FIELDS_TABLE, null, values);
db.close(); // Closing database connection
}
}
I get Error Inserting to database with this way:
db.addToReceived(new ReceiveFields(
Long.valueOf(str1[0]),
str1[1],
str1[2],
URLDecoder.decode(str1[3], "UTF-8"),
URLDecoder.decode(str1[4], "UTF-8"),
ct.getGregorianDate()));
Log.i Result:
29904757 30007227 00120504001 00120504001 sssasaS 2014/8/3
full LogCate Result:
08-28 06:35:29.974 2698-2698/ir.tsms E/Database﹕ Error inserting lastId=29904755 receiveDate=2014/8/3 senderName=09127574751 mobileNumber=09127574751 smsNumber=30007227 smsBody=
android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
at ir.tsms.DataBase.DatabaseHandler.addToReceived(DatabaseHandler.java:96)
at ir.tsms.wsdl.TSMS.getReceivedSMS(TSMS.java:141)
at ir.tsms.Receive.ResivedSMS.getResivedSMS(ResivedSMS.java:41)
at ir.tsms.Receive.ResivedSMS.<init>(ResivedSMS.java:36)
at ir.tsms.Activities.DashboardActivity.onTabSelected(DashboardActivity.java:160)
at android.support.v7.app.ActionBarImplBase.selectTab(ActionBarImplBase.java:486)
at android.support.v7.app.ActionBarImplBase.addTab(ActionBarImplBase.java:410)
at android.support.v7.app.ActionBarImplBase.addTab(ActionBarImplBase.java:401)
at ir.tsms.Activities.DashboardActivity.onCreate(DashboardActivity.java:68)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
at ir.tsms.wsdl.TSMS.getReceivedSMS(TSMS.java:141) line is :
db.addToReceived(new ReceiveFields(
Check these things, it may causes by these reason:
1- you did not add COLUMN_ID to values so it is null.
2-COLUMN_ID and COLUMN_LASTID are not UNIQUE (clear your database or android app data , maybe it stored in your db first time you ran it and now again you want to add you can not because it is not unique now, every time you run, your db is in memory and it is not clread, in order to clear it you must remove app data or run delete SQL statement)
so i suggest you to see your DB file, if you use eclipse try this:
http://www.tylerfrankenstein.com/browse-android-emulator-sqlite-database-eclipse
3-COLUMN_ID and COLUMN_LASTID are not int i mean fields.getLastId() may return String
check exactly your table defination and your values to insert !!!
It seems like you're trying to insert null as smsBody but your table does not allow you to do this as it has NOT NULL constraint for this column.