App crashes on this.getWritableDatabase(); [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
The following code is a function in my dbhandler. This works fine on android studio emulator but crashes on my actual mobile. If I comment out the line SQLiteDatabase db = this.getWritableDatabase(); then it works fine. What would cause this?
public ArrayList<Integer> getCategories(int ParID){
ArrayList<Integer> catAr = new ArrayList<Integer>();
catAr.add(1);
//loop thru all cats and store id's in catAr
String query = "Select * FROM " + TABLE_PRODUCTS;
SQLiteDatabase db = this.getWritableDatabase();
db.close();
return catAr;
}

Please confirm if the DB exists or not. As mentioned in the docs this method could throw a SQLiteException if the database cannot be opened for writing.
Also please enclose this method use in try/catch block so that it is easier to debug and fails gracefully without crashing the app.

You might change your database, and in emulator it reads from old version. if you change database version(in your custom SQLiteOpenHelper class) then you might see that error in emulator too:
super(context, DB_NAME, null, DB_VERSION);
for example change DB_VERSION from 1 to 2.

Related

Searching MongoDB using NetBeans [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I have a project that's due in 3 days and this is the last step and I can't make it work. I connected MongoDB to NetBeans by downloading mongo-java-driver-2.13.3.
I have a homepage from which user will enter information in any of the fields (street, borough, cuisine, grade, name) and you must display the result back to user. If no restaurant matches the entered information, display a message to the user that no result. My code prints all info. I want it to be in text area and only print selected info.
Here is what I wrote:
private void btnfindActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:
DBCursor cursor =table.find();
while (cursor.hasNext())
{
//jTextArea1.setText(jTextArea1.getText()+"\n"+cursor.next());
System.out.println(cursor.next());
}
}
Maybe you need to do something like that...
while (cursor.hasNext()){
BasicDBObject object = (BasicDBObject) cursor.next();
String name = object.getString("name_of_column");
jTextArea1.setText(name);
https://www.baeldung.com/java-mongodb
mongodb - java get field value

My SQLite in android studio with DISTINCT order question [closed]

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
This is my database table call "BREAKFAST_TABLE" I want to keep date that not duplicate to list
This is my code
public List<String> get_notSameDate(String status)
{
List<String> return_Food=new ArrayList<>();
//COLUMN_DATE is "DATE"
//status is "BREAKFAST_TABLE"
String queryString="SELECT DISTINCT"+COLUMN_DATE+" FROM "+status;
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor = db.rawQuery(queryString,null);
if(cursor.moveToFirst())
{
do {
String date=cursor.getString(0);
return_Food.add(date);
}while (cursor.moveToNext());
}
cursor.close();
db.close();
return return_Food;
}
I using this code it always crashes.
Though we don't know what error it crashes with, I believe it fails because of missing space symbol between "DISTINCT" and the column name:
"SELECT DISTINCT"+COLUMN_DATE+" FROM "+status;
fixed:
"SELECT DISTINCT "+COLUMN_DATE+" FROM "+status;

JDBC delete method seems to not be working in my Spring Boot service [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm trying to create a simple JDBC method to delete from my DB, and I'm not sure if I'm going about this the correct way. This is inside one of my services.
Method:
public void deleteLocation(Integer id) {
String DELETE = "DELETE FROM locale WHERE id=?";
namedParameterJdbcTemplate.update(DELETE, new BeanPropertySqlParameterSource(id));
}
I would try changing your update line to
namedParameterJdbcTemplate.update(DELETE, id);.
If you are using spring-boot. Then you should be using spring-data-jpa to manage your database. Jdbc is Hard way of doing this.
If you are using jdbc delete to a specific row use prepared statement. You can refer this:
preparedStatement = connection.prepareStatement("DELETE * from table_name WHERE id= ?");
preparedStatement.setInt(1, id);
return !preparedStatement.execute();

Hibernate: HQL is not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I wrote a SQL query in my hibernate application using where clause and and .
Here is method ,
public List<Comobility> getComobilityByIdComobilityItemsAndIdPatient(int idComobilityItems, int idPatient, Session session) {
Query query = session.createQuery("from Comobility where comobility_items_idcomobility_items= :idComobilityItems and patient_idpatient= :idpatinet");
query.setParameter("idComobilityItems", idComobilityItems);
query.setParameter("idpatinet", idPatient);
List<Comobility> list = query.list();
return list;
}
But this is not working . There is no any exception or error . Actually, there is no any result.
Have any ideas ?
You should try like that
Wrap your method with try catch block. I believe the query has an error then it will enter to catch block
public List<Comobility>getComobilityByIdComobilityItemsAndIdPatient(int idComobilityItems, int idPatient, Session session) {
try {
logger.debug("xxxx")
//do comething
} catch (Exception e) {
e.printStackTrace();
}
}
Make sure your method parameters are not null
Check your list is empty or not.
For checking you can use IDE debugger or put some logs in your method.
You need to check the generated SQL in the log and execute it against database. If query is compiled and no records are returned, means there are no records for the particular criteria.
To generate the SQL by Hibernate, please set property show_sql to true in hibernate configuration.

Android-Quiz App with SqliteAssetHelper

I've started to develop a quiz application (multiple and single choice) for android.
The reason why I chose the SqliteAssetHelper library is, that I want a prepopulated database.
I've created a sqlite database with 2 tables (question & answer).
With the following method in my database class I'm getting the question items:
public Cursor getQuestionItems() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(TABLES.QUESTION);
Cursor questionCursor = qb.query(db, null, null, null, null, null, null);
return questionCursor;
}
In the main class (in onCreate) I'm creating question objects and add them to the arraylist:
Cursor questionCursor = db.getQuestionItems();
while (questionCursor.moveToNext()) {
Question question = new Question();
question.setId(
questionCursor.getString(questionCursor.getColumnIndexOrThrow(MyDatabase.QuestionColumns.ID)));
question.setQuestiontext(questionCursor
.getString(questionCursor.getColumnIndexOrThrow(MyDatabase.QuestionColumns.QUESTIONTEXT)));
question.setQuestiontype(questionCursor
.getInt(questionCursor.getColumnIndexOrThrow(MyDatabase.QuestionColumns.QUESTIONTYPE)));
questionList.add(question);
}
questionCursor.close();
I'm doing the same also for the answer items. Now I want to show a view with a question and the possible answers (for example 4 answers). If the user clicks on the "next"-button the next question with the answers should be displayed.
How can I achieve this efficiently? Is it at all necessary to create question and answer items? I'm not sure if this is the right way.
Thanks in advance!
To follow database rules, we should do like that. One table for questions, one table for answers as per the structure provided by you. Its the better way, because you will follow the database rule and there will not be redudant data. But when displaying the answer, display the data for proper question ID.

Categories