Query not working

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Query not working

Postby SamB09 » Mon Jan 18, 2010 7:07 pm

Im trying to send a raw query to the database and then display the results . I call the raq query which is in the method getallTitles from a class called useDB. I get the force close error . Im guessing something is wrong with the getallTitles method or from where im calling it in the useDB class. Can anyone help?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package project.Maps;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.content.ContentValues;
  9.  
  10. import android.content.Context;
  11.  
  12. import android.database.Cursor;
  13.  
  14. import android.database.SQLException;
  15.  
  16. import android.database.sqlite.SQLiteDatabase;
  17.  
  18. import android.database.sqlite.SQLiteOpenHelper;
  19.  
  20. import android.util.Log;
  21.  
  22. public class dbAdapter  extends Activity {
  23.  
  24.  
  25.  
  26.     public static final String KEY_ROWID = "_id";
  27.  
  28.     public static final String KEY_Artist = "Artist";
  29.  
  30.     public static final String KEY_Stage = "Stage";
  31.  
  32.     public static final String KEY_Day = "Day";
  33.  
  34.  
  35.  
  36.     private static final String TAG = "DBAdapter";
  37.  
  38.    
  39.  
  40.     private static final String DATABASE_NAME = "Festival";
  41.  
  42.     private static final String DATABASE_TABLE = "titles";
  43.  
  44.  
  45.  
  46.     private static final int DATABASE_VERSION = 8;
  47.  
  48.  
  49.  
  50.     private static final String DATABASE_CREATE =
  51.  
  52.         "create table titles (_id integer primary key autoincrement, "
  53.  
  54.         + "Artist text not null, Stage text not null, "
  55.  
  56.         + "Day text not null);";
  57.  
  58.        
  59.  
  60.        
  61.  
  62.     private final Context context;
  63.  
  64.    
  65.  
  66.     private DatabaseHelper DBHelper;
  67.  
  68.     private SQLiteDatabase db;
  69.  
  70.  
  71.  
  72.     public dbAdapter(Context ctx)
  73.  
  74.     {
  75.  
  76.         this.context = ctx;
  77.  
  78.         DBHelper = new DatabaseHelper(context);
  79.  
  80.     }
  81.  
  82.        
  83.  
  84.     private static class DatabaseHelper extends SQLiteOpenHelper
  85.  
  86.     {
  87.  
  88.         DatabaseHelper(Context context)
  89.  
  90.         {
  91.  
  92.             super(context, DATABASE_NAME, null, DATABASE_VERSION);
  93.  
  94.         }
  95.  
  96.  
  97.  
  98.         @Override
  99.  
  100.         public void onCreate(SQLiteDatabase db)
  101.  
  102.         {
  103.  
  104.             db.execSQL(DATABASE_CREATE);
  105.  
  106.            
  107.  
  108.         }
  109.  
  110.  
  111.  
  112.         @Override
  113.  
  114.         public void onUpgrade(SQLiteDatabase db, int oldVersion,
  115.  
  116.         int newVersion)
  117.  
  118.         {
  119.  
  120.             Log.w(TAG, "Upgrading database from version " + oldVersion
  121.  
  122.                     + " to "
  123.  
  124.                     + newVersion + ", which will destroy all old data");
  125.  
  126.             db.execSQL("DROP TABLE IF EXISTS Artists");
  127.  
  128.             onCreate(db);
  129.  
  130.         }
  131.  
  132.     }    
  133.  
  134.    
  135.  
  136.     //---opens the database---
  137.  
  138.     public dbAdapter open() throws SQLException
  139.  
  140.     {
  141.  
  142.         db = DBHelper.getWritableDatabase();
  143.  
  144.         return this;
  145.  
  146.     }
  147.  
  148.  
  149.  
  150.     //---closes the database---    
  151.  
  152.     public void close()
  153.  
  154.     {
  155.  
  156.         DBHelper.close();
  157.  
  158.         db.close();
  159.  
  160.     }
  161.  
  162.    
  163.  
  164.     //---insert a title into the database---
  165.  
  166.     public long insertTitle(String isbn, String title, String publisher)
  167.  
  168.     {
  169.  
  170.         ContentValues initialValues = new ContentValues();
  171.  
  172.         initialValues.put(KEY_Artist, isbn);
  173.  
  174.         initialValues.put(KEY_Stage, title);
  175.  
  176.         initialValues.put(KEY_Day, publisher);
  177.  
  178.         return db.insert(DATABASE_TABLE, null, initialValues);
  179.  
  180.        
  181.  
  182.     }
  183.  
  184.     // insert into table 2
  185.  
  186.    
  187.  
  188.     //---retrieves all the titles---
  189.  
  190.     public  Cursor getAllTitles()
  191.  
  192.     {
  193.  
  194.      return  db.rawQuery( " SELECT Artist FROM Artists", null);
  195.  
  196.        
  197.  
  198.     }
  199.  
  200.  
  201.  
  202.    
  203.  
  204.     //---retrieves a particular title---
  205.  
  206.     public Cursor getTitle(long rowId) throws SQLException
  207.  
  208.     {
  209.  
  210.         Cursor mCursor =
  211.  
  212.                 db.query(true, DATABASE_TABLE, new String[] {
  213.  
  214.                                 KEY_ROWID,
  215.  
  216.                                 KEY_Artist,
  217.  
  218.                                 KEY_Stage,
  219.  
  220.                                 KEY_Day
  221.  
  222.                                 },
  223.  
  224.                                 KEY_ROWID + "=" + rowId,
  225.  
  226.                                 null,
  227.  
  228.                                 null,
  229.  
  230.                                 null,
  231.  
  232.                                 null,
  233.  
  234.                                 null);
  235.  
  236.         if (mCursor != null) {
  237.  
  238.             mCursor.moveToFirst();
  239.  
  240.         }
  241.  
  242.         return mCursor;
  243.  
  244.     }
  245.  
  246.  
  247.  
  248. }
Parsed in 0.045 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. ckage project.Maps;
  3.  
  4.  
  5.  
  6. import java.util.ArrayList;
  7.  
  8.  
  9.  
  10. import android.app.Activity;
  11.  
  12. import android.app.ListActivity;
  13.  
  14. import android.database.Cursor;
  15.  
  16. import android.os.Bundle;
  17.  
  18. import android.widget.ArrayAdapter;
  19.  
  20. import android.widget.Toast;
  21.  
  22.  
  23.  
  24. public class useDB extends ListActivity {
  25.  
  26.     /** Called when the activity is first created. */
  27.  
  28.     @Override
  29.  
  30.     public void onCreate(Bundle savedInstanceState)
  31.  
  32.     {
  33.  
  34.         super.onCreate(savedInstanceState);
  35.  
  36.         setContentView(R.layout.main);
  37.  
  38.         dbAdapter db = new dbAdapter(this);
  39.  
  40.      
  41.  
  42.    
  43.  
  44.  
  45.  
  46.  
  47.  
  48.         //---get all titles---
  49.  
  50.         db.open();     
  51.  
  52.         Cursor c =  db.getAllTitles();
  53.  
  54.        
  55.  
  56.         if (c.moveToFirst())
  57.  
  58.         {
  59.  
  60.             do {          
  61.  
  62.                 DisplayTitle(c);
  63.  
  64.             } while (c.moveToNext());
  65.  
  66.         }
  67.  
  68.         db.close();
  69.  
  70.     }    
  71.  
  72.     ArrayList<String> results = new ArrayList<String>();
  73.  
  74.  
  75.  
  76.     public void DisplayTitle(Cursor c)
  77.  
  78.     {
  79.  
  80.         String col1 =  c.getString(1);
  81.  
  82.         //String col2 =  c.getString(1);
  83.  
  84.         //String col3 =  c.getString(2);
  85.  
  86.         //String col4 =  c.getString(3);
  87.  
  88.  
  89.  
  90.  
  91.  
  92.         results.add(col1 );
  93.  
  94.  
  95.  
  96.              
  97.  
  98.                 this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
  99.  
  100.     }
  101.  
  102.  
  103.  
  104. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Top

Postby qlimax » Mon Jan 18, 2010 7:34 pm

"select Artist from titles"
:lol:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby SamB09 » Mon Jan 18, 2010 7:39 pm

Lol thanks for pointing that out, i corrected it but am still getting the same error :( .
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Postby qlimax » Mon Jan 18, 2010 7:56 pm

correct that also:
db.execSQL("DROP TABLE IF EXISTS Artists");

anyway,
if you watch in the logcat view you will find more useful information about your error.
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby qlimax » Mon Jan 18, 2010 7:59 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public void DisplayTitle(Cursor c)
  2.  
  3.     {
  4.  
  5.      String col1 =  c.getString(1);
  6.  
  7.      //String col2 =  c.getString(1);
  8.  
  9.      //String col3 =  c.getString(2);
  10.  
  11.      //String col4 =  c.getString(3);
  12.  
  13.  
  14.  
  15.  
  16.  
  17.      results.add(col1 );
  18.  
  19.  
  20.  
  21.              
  22.  
  23.                 this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
  24.  
  25.     }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4



put this.setListadapter(...) outside the method.
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby SamB09 » Mon Jan 18, 2010 8:33 pm

Here is log cat- it sais something about array adapter ive highlighted what i think the necessary parts are.


01-18 19:11:34.437: WARN/System.err(708): Can't dispatch DDM chunk 4d505251: no handler defined
01-18 19:11:34.737: DEBUG/AndroidRuntime(708): Shutting down VM
01-18 19:11:34.737: WARN/dalvikvm(708): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
01-18 19:11:34.747: ERROR/AndroidRuntime(708): Uncaught handler: thread main exiting due to uncaught exception
01-18 19:11:34.798: ERROR/AndroidRuntime(708): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{project.Maps/project.Maps.useDB}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at android.os.Looper.loop(Looper.java:123)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at android.app.ActivityThread.main(ActivityThread.java:3948)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at java.lang.reflect.Method.invoke(Method.java:521)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at dalvik.system.NativeStart.main(Native Method)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at android.app.Activity.getSystemService(Activity.java:3242)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at [b]android.widget.ArrayAdapter.init(ArrayAdapter.java:271)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:150)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at project.Maps.useDB.<init>(useDB.java:51)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at java.lang.Class.newInstanceImpl(Native Method)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at java.lang.Class.newInstance(Class.java:1472)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at android.app.Instrumentation.newActivity(Instrumentation.java:1097)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2186)
01-18 19:11:34.798: ERROR/AndroidRuntime(708): ... 11 more
01-18 19:11:34.827: INFO/Process(569): Sending signal. PID: 708 SIG: 3
01-18 19:11:34.837: INFO/dalvikvm(708): threadid=7: reacting to signal 3
01-18 19:11:34.918: INFO/dalvikvm(708): Wrote stack trace to '/data/anr/traces.txt'
01-18 19:11:35.157: INFO/ARMAssembler(569): generated scanline__00000077:03515104_00000000_00000000 [ 27 ipp] (41 ins) at [0x280d58:0x280dfc] in 6759535 ns
01-18 19:11:43.622: WARN/ActivityManager(569): Launch timeout has expired, giving up wake lock!
01-18 19:11:43.647: DEBUG/KeyguardViewMediator(569): pokeWakelock(5000)
01-18 19:11:43.787: INFO/ARMAssembler(569): generated scanline__00000077:03545404_00000A04_00000000 [ 29 ipp] (51 ins) at [0x279950:0x279a1c] in 1684996 ns
01-18 19:11:44.537: WARN/ActivityManager(569): Activity idle timeout for HistoryRecord{4364b588 {project.Maps/project.Maps.useDB}}
01-18 19:11:46.027: INFO/Process(708): Sending signal. PID: 708 SIG: 9
01-18 19:11:46.047: INFO/ActivityManager(569): Process project.Maps (pid 708) has died.
01-18 19:11:46.377: INFO/ActivityManager(569): Start proc com.android.inputmethod.latin for service com.android.inputmethod.latin/.LatinIME: pid=715 uid=10003 gids={3003}
01-18 19:11:46.377: INFO/ARMAssembler(569): generated scanline__00000077:03515104_00001001_00000000 [ 64 ipp] (84 ins) at [0x281040:0x281190] in 2245194 ns
01-18 19:11:46.637: INFO/jdwp(715): received file descriptor 20 from ADB
01-18 19:11:46.817: WARN/System.err(715): Can't dispatch DDM chunk 46454154: no handler defined
01-18 19:11:46.817: WARN/System.err(715): Can't dispatch DDM chunk 4d505251: no handler defined
01-18 19:11:47.217: DEBUG/dalvikvm(715): Trying to load lib /system/lib/libjni_latinime.so 0x43597ee8
01-18 19:11:47.237: DEBUG/dalvikvm(715): Added shared lib /system/lib/libjni_latinime.so 0x43597ee8
01-18 19:11:51.757: DEBUG/dalvikvm(608): GC freed 3984 objects / 227128 bytes in 151ms
01-18 19:11:56.798: DEBUG/dalvikvm(635): GC freed 2850 objects / 162256 bytes in 179ms
01-18 19:12:05.297: DEBUG/dalvikvm(657): GC freed 2807 objects / 172720 bytes in 173ms
01-18 19:12:10.277: DEBUG/dalvikvm(666): GC freed 1279 objects / 81200 bytes in 134ms
[/
b]
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Top

Postby SamB09 » Mon Jan 18, 2010 9:43 pm

Can anyone help?
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Postby qlimax » Mon Jan 18, 2010 9:49 pm

wait...
:P
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby qlimax » Mon Jan 18, 2010 9:51 pm

can you try if this works?


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3.     public void onCreate(Bundle savedInstanceState)
  4.  
  5.     {
  6.  
  7.         super.onCreate(savedInstanceState);
  8.  
  9.         setContentView(R.layout.main);
  10.  
  11.         dbAdapter db = new dbAdapter(this);
  12.  
  13.      
  14.  
  15.    
  16.  
  17.  
  18.  
  19.  
  20.  
  21.         //---get all titles---
  22.  
  23.         db.open();
  24.  
  25.         Cursor c =  db.getAllTitles();
  26.  
  27.        
  28.  
  29.         if (c.moveToFirst())
  30.  
  31.         {
  32.  
  33.             do {          
  34.  
  35.                  String col1 =  c.getString(1);
  36.  
  37.              //String col2 =  c.getString(1);
  38.  
  39.             //String col3 =  c.getString(2);
  40.  
  41.             //String col4 =  c.getString(3);
  42.  
  43.  
  44.  
  45.  
  46.  
  47.               results.add(col1 );
  48.  
  49.             } while (c.moveToNext());
  50.  
  51.         }
  52.  
  53.         db.close();
  54.  
  55.  
  56.  
  57. this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
  58.  
  59.  
  60.  
  61.        
  62.  
  63.     }    
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.     ArrayList<String> results = new ArrayList<String>();
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby SamB09 » Mon Jan 18, 2010 10:01 pm

No that doesn't work , thanks though. I really dont know what is wrong with it.
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Postby qlimax » Mon Jan 18, 2010 10:03 pm

can you attach a zip file of your project folder?
it will be faster and easier :)
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby SamB09 » Mon Jan 18, 2010 10:08 pm

Sure, ill just tell you what im trying to do. I want to have another class like UseDB which will call a query method from the adapter class, then ill hook them up to buttons which is the easy bit .
Attachments
Mapping.zip
(32.16 KiB) Downloaded 67 times
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Postby qlimax » Mon Jan 18, 2010 10:34 pm

ok
I send you the project that work
what you have to do now is detele your current database, a new one will be created when you will run the app

to delete the database use adb shell

then go to /data/data/project.Maps/databases

and remove the file named festival (the db)


ok?
Attachments
Mapping.zip
(32.5 KiB) Downloaded 79 times
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby SamB09 » Mon Jan 18, 2010 10:56 pm

Hi ive opened the adb shell but am not sure how to get to that file ? Do i get there with a command in the adb shell?Im new to android if you hadnt guessd :lol:

Thanks
SamB09
Developer
Developer
 
Posts: 39
Joined: Tue Oct 27, 2009 9:22 pm
Location: England

Postby qlimax » Mon Jan 18, 2010 10:59 pm

Code: Select all
C:\android-sdk-windows\tools>adb.exe shell
# cd /data/data/project.Maps/databases
cd /data/data/project.Maps/databases
# ls
ls
Festival
# rm Festival
rm Festival
# ls
ls
#
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top
Next

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 6 guests