I have been using the notepad SQLhelper (notesDBadapter) as a model, some of it works, some doesn't. I can get a cursor for 'fetchallrecords() but it crashes if I try a call passing an argument and using the 'WHERE'. The argument is passed but the cursor fails. My code in activity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listselectedfile);
//Button ID clicked in previous activity
Bundle bundle = getIntent().getExtras();
int BtnId = bundle.getInt("ButtonID");
Toast.makeText(this, "ButtonID selected in Main:= " + BtnId, Toast.LENGTH_LONG) .show();
mDbHelper = new SectionsDbAdapter(this);
mDbHelper.open();
fillData();
}
private void fillData() {
// Get all of the notes from the database and create the item list
//Cursor c = mDbHelper.fetchAllRecords(); <=== works fine
Cursor c = mDbHelper.fetchRecordsbySource("UK"); <=== fails in DBhelper
startManagingCursor(c);
String[] from = new String[] { SectionsDbAdapter.KEY_DESC };
//String[] from = new String[] { SectionsDbAdapter.KEY_SOURCE }; <=== can fetch this column from table
int[] to = new int[] { R.id.tv_full_width }; //the R.id.xxx= view in the .xml file
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter records =
new SimpleCursorAdapter(this, R.layout.section_row_full_width, c, from, to); //the .xml file containing the R.id.xxxx
setListAdapter(records);
}
In the DBhelper;
This call works and returns the full table;
public Cursor fetchAllRecords() {
return mDb.query(DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_DESC, KEY_DEPTH, KEY_TWEB,
KEY_BF1, KEY_TF1, KEY_BF2, KEY_TF2,
KEY_IMAJOR, KEY_IMINOR,
KEY_PLMAJOR, KEY_PLMINOR,
KEY_JTORSION, KEY_AREA,
KEY_WARPING, KEY_CYCHANNEL,
KEY_SHAPE, KEY_SOURCE,
KEY_UNITS},null, null, null, null, null);
}
This call fails on the cursor (the argument is passed successfully);
public Cursor fetchRecordsbySource(String source) throws SQLException {
Log.v("In fetchRecordsbySource", "source = " +source);
Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_DESC}, KEY_SOURCE + " = " + source, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
There is nothing obvious to me in the Eclipse debug, but, as a newby, I probably have not got the necessary perspective.
Can anyone spot the error?
In the code you have, if source is a non-numeric value, it needs to surrounded by single quotes, like this:
KEY_ROWID, KEY_DESC}, KEY_SOURCE + " = '" + source + "'", null, null, null, null, null)
But you will be better off if you pass your filter arguments as ?; this avoids (among other things) SQL injection attacks
KEY_ROWID, KEY_DESC}, KEY_SOURCE + " = ?", new String[] { source }, null, null, null, null)
Related
i have created this method in dbHelper class.
I am calling this method from mainActivity using onClickListener to show column 1 of each row one by one by changing cursor position.
i need to move the cursor to next row each time the button is clicked to show the string in Column1 in a textView.
i am new to android and programming too, so please bear with me.
public String nextData() {
String nQuote = "";
int i=0;
String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_IQ };
Cursor resultSet = ourDatabase.query(DATABASE_TABLE, columns, null, null,
null, null, null);
resultSet.movetoPostion(i);
if (i==0||i!=resultSet.getcount()){
resultSet.moveToNext();
nQuote = resultSet.getString(1);
i++;
}
return nQuote;
}
use this,
public String nextData(int position) {
String nQuote = "";
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_IQ};
Cursor resultSet = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
if (position >= 0 && position < resultSet.getCount()) {
resultSet.moveToPosition(position);
nQuote = resultSet.getString(1);
}
resultSet.close();
return nQuote;
}
I want only particular rows that has "E" inside the column "TX_IDT". I used the following code but apps stops. In logcat the error says it is at db.query line.
public Cursor getAllRows( ) {
String where = null;
SQLiteDatabase db = helper.getReadableDatabase();
String[] columns = { VivzHelper.UID, helper.UID,helper.NAME,helper.TX_IDT};
String whereClause = "TX_IDT = ? ";
String[] whereArgs = new String[] { "E" };
Cursor c = db.query( VivzHelper.TABLE_NAME, columns,whereClause,whereArgs, null, null, NAME + " ASC"); // for out btn
if (c != null) {
c.moveToFirst();
}
return c;
}
`
Seems like you want record's containing "E" ,
Try this
Cursor c = db.query(VivzHelper.TABLE_NAME, columns, helper.TX_IDT +" LIKE '%E%' ", null, null, null, null);
Adapter.java
public String getID(String i) throws SQLException
{
db=DBHelper.getReadableDatabase();
String ij="No Track Found";
Cursor mCursor =
db.query(true, TABLE, new String[] {KEY_ID}, KEY_NAME + "=" + "'"+i+"'", null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
if (mCursor.moveToFirst())
{
ij=mCursor.getString(0);
}
return ij ;
}
The Question is when I pass any song's name that contain "'"(e.g. alexander's blade) , it throws and error. Otherwise all fine.
You should use the selectionArgs parameter:
db.query(true, //distinct
TABLE, //table
new String[] {KEY_ID}, //columns
KEY_NAME + "=?", //selection
new String[] {i}, //selectionArgs
null, //groupBy
null, //having
null, //orderBy
null //limit
);
Using selectionArgs also help to prevent SQL Injection
You need to call your method like this:
Cursor c = bd.query(Constantes.TABELA_APLICATIVO, COLS, "urlandroid = ?", new String[]{url}, null, null, null);
The caracter "?" represents the values on Third parameter(String[]).
Followed this tutorial, faced cursor problem in SimpleCursorAdapter. In tutorial example working as intended. In my code it is showing an error "The constructor SimpleCursorAdapter is undefined". No idea what was undefined. Code look like this:
Cursor cursor = null;
if (inputText == null || inputText.length() == 0) {
cursor = myDataBase.query(true, GL_TABLE, new String[] { GL_ID,
GL_FK, GL_LANG, GL_VALUE}, GL_FK
+ " like '%" + inputText + "%'", null, null, null, null,
null);
} else {
cursor = myDataBase.query(true, GL_TABLE, new String[] { GL_ID,
GL_FK, GL_LANG, GL_VALUE}, GL_VALUE
+ " like '%" + inputText + "%'", null, null, null, null,
null);
}
if (cursor != null) {
cursor.moveToFirst();
}
String[] columns = new String[] { GL_FK, GL_LANG, GL_VALUE};
int[] to = new int[] { R.id.tvWord, R.id.tvMeaning, R.id.tvKanji};
dataAdapter = new SimpleCursorAdapter(this, R.layout.listword,
cursor, columns, to, 0);
Did you import the class ?
import android.widget.SimpleCursorAdapter;
If you did, then be sure to call the constructor with the right kind of parameters. "this" should stand for an activity or a context. if you are in a Runnable or in a clicklistener you will have to call the super MyActivty.this
I understand the requery() mechanic, but i do not understand the implementation:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
datasource = new pollDataSource(this);
datasource.open();
Cursor values = datasource.getAllCategorie();
String[] categorieColumns =
{
MySQLiteHelper.COLUMN_NOME // Contract class constant containing the word column name
};
int[] mWordListItems = { R.id.categoria_label };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getApplicationContext(), // The application's Context object
R.layout.single_list_item, // A layout in XML for one row in the ListView
values, // The result from the query
categorieColumns, // A string array of column names in the cursor
mWordListItems, // An integer array of view IDs in the row layout
0); // Flags (usually none are needed)
setListAdapter(adapter);
}
public void onClick(View view) {
categorie categoria = null;
switch (view.getId()) {
case R.id.add:
categoria = datasource.createCategoria("pluto") ;
break;
case R.id.categoria_label:
break;
}
}
after the categoria = datasource.createCategoria("pluto"); i have to define another:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getApplicationContext(), // The application's Context object
R.layout.single_list_item, // A layout in XML for one row in the ListView
values, // The result from the query
categorieColumns, // A string array of column names in the cursor
mWordListItems, // An integer array of view IDs in the row layout
0); // Flags (usually none are needed)
setListAdapter(adapter);
PollDataSource:
public class pollDataSource {
// Database fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allCategorieColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_PREF, MySQLiteHelper.COLUMN_NOME };
private String[] allSondaggiColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_CATID, MySQLiteHelper.COLUMN_DOMANDA };
private String[] allRisposteColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_SONDID, MySQLiteHelper.COLUMN_RISPOSTA,
MySQLiteHelper.COLUMN_SELEZIONATA };
public pollDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public categorie createCategoria(String categoria) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_NOME, categoria);
values.put(MySQLiteHelper.COLUMN_PREF, 0);
long insertId = database.insert(MySQLiteHelper.TABLE_CATEGORIE, null,
values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_CATEGORIE,
allCategorieColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
categorie newCategoria = cursorToCategorie(cursor);
cursor.close();
return newCategoria;
}
public void deleteCategoria(categorie categoria) {
long id = categoria.getId();
System.out.println("Categoria cancellata, id: " + id);
database.delete(MySQLiteHelper.TABLE_CATEGORIE, MySQLiteHelper.COLUMN_ID
+ " = " + id, null);
}
public sondaggi createSondaggio(String domanda, int catid) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_DOMANDA, domanda);
values.put(MySQLiteHelper.COLUMN_CATID, catid);
long insertId = database.insert(MySQLiteHelper.TABLE_SONDAGGI, null,
values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_SONDAGGI,
allSondaggiColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
sondaggi newSondaggio = cursorToSondaggi(cursor);
cursor.close();
return newSondaggio;
}
public void deleteSondaggio(sondaggi sondaggio) {
long id = sondaggio.getId();
System.out.println("Sondaggio cancellato, id: " + id);
database.delete(MySQLiteHelper.TABLE_SONDAGGI, MySQLiteHelper.COLUMN_ID
+ " = " + id, null);
}
public Cursor getAllCategorie() {
List<categorie> categorie = new ArrayList<categorie>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_CATEGORIE,
allCategorieColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
categorie categoria = cursorToCategorie(cursor);
categorie.add(categoria);
cursor.moveToNext();
}
// Make sure to close the cursor
// cursor.close();
return cursor;
}
private categorie cursorToCategorie(Cursor cursor) {
categorie categorie = new categorie();
categorie.setId(cursor.getLong(0));
categorie.setPreferita(cursor.getLong(1));
categorie.setNome(cursor.getString(2));
return categorie;
}
private sondaggi cursorToSondaggi(Cursor cursor) {
sondaggi sondaggi = new sondaggi();
sondaggi.setId(cursor.getLong(0));
sondaggi.setDomanda(cursor.getString(1));
sondaggi.setCatid(cursor.getLong(2));
return sondaggi;
}
}
with the same inputs?? so basically I will have the same exact code in two different places of the same class... May I define a... "procedure" or a class? I'm sorry if I'm too naive, I'm a real noob at this :D
the requery cursor method is deprecated. but you're right you have to reload the cursor and then make sure that your new cursor is being presented to your ListView. for a quick 'n dirty solution, I suggest you make your adapter a field. and then use the following method:
private void requery() {
Cursor values = datasource.getAllCategorie();
adapter.changeCursor(values);
}
What it does is remake a cursor. then by calling changeCursor you swap the old cursor for the new one and the old one is closed automatically. at the very least, you save yourself from making a new adapter. The more sophicated approach with all the trim and fixes i suppose would be to implement a CursorLoader which would perform the process automatically when the database content has been changed.