SQLite database problem

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

SQLite database problem

Postby totw » Tue Mar 31, 2009 11:00 am

I'm trying to create a database for our application with class name DataBaseWork with code :


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DataBaseWork extends ListActivity {
  2.  
  3.         public static final String MY_DATABASE_NAME = "ActivityLog";
  4.  
  5.         public static final String MY_DATABASE_TABLE = "logtable";
  6.  
  7.         public static SQLiteDatabase myDB = null;
  8.  
  9.         /** Called when the activity is first created. */
  10.  
  11.     DataBaseWork(){
  12.  
  13.                
  14.  
  15.          try{
  16.  
  17.                  
  18.  
  19.                  this.createDatabase(MY_DATABASE_NAME, 1, MODE_PRIVATE, null);
  20.  
  21.                  myDB = this.openDatabase(MY_DATABASE_NAME, null);
  22.  
  23.                
  24.  
  25.                  myDB.execSQL("CREATE TABLE IF NOT EXISTS "
  26.  
  27.                         + MY_DATABASE_TABLE
  28.  
  29.                         + " (Date VARCHAR, serverIP VARCHAR ;");
  30.  
  31.  
  32.  
  33.          } catch (FileNotFoundException e) {
  34.  
  35.          }
  36.  
  37.          
  38.  
  39.                
  40.  
  41.     }
  42.  
  43.    
  44.  
  45.     public void addentry(String servIP)
  46.  
  47.     {
  48.  
  49.         Date d = new Date();
  50.  
  51.         String cdate = new String(d.toString());
  52.  
  53.        
  54.  
  55.    
  56.  
  57.         myDB.execSQL("INSERT INTO "
  58.  
  59.                 + MY_DATABASE_TABLE
  60.  
  61.                 + " (Date, serverIP)"
  62.  
  63.                 + " VALUES (cdate, servIP);");
  64.  
  65.  
  66.  
  67.     }
  68.  
  69. }
  70.  
  71.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


I tried to create an object of the above class in java file main as follows :

DataBaseWork dbw;
dbw = new DataBaseWork();

I'm getting the following error :
unable to start activity ComponentInfo{tel.net.client/tel.net.client.Main}:
java.lang.RuntimeException : not supported in system context.

I am sure that this error is because of the database work because after removing that piece of code the program works fine.

PS: I'm using the very first android SDK (m3r20a) and finding it convenient to use the same rather than upgrading to the latest version.
User avatar
totw
Developer
Developer
 
Posts: 27
Joined: Tue Mar 31, 2009 10:35 am

Top

Postby stifry » Wed Apr 01, 2009 6:10 pm

You will want to do this in the onCreate(). Android Activity classes don't use constructors to initialize. You might want to check out the Activity lifecycle:

Activity Lifecycle

onCreate() Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.

Always followed by onStart().
Visit Trackaroo.com!
Trackmaster - Motorsports Lap Timer
Dynomaster - Performance Dyno
Image Image
User avatar
stifry
Developer
Developer
 
Posts: 46
Joined: Tue Feb 17, 2009 7:50 pm

Postby WarrenFaith » Thu Apr 02, 2009 12:01 pm

Well I doubt that an Activity is the place to handle SQLite... Check out the notepad tutorial for more information.
:arrow: http://www.anddev.org/viewtopic.php?p=20070#20070
I posted the main classes to handle database access (fully inspired by notepad)
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Postby totw » Thu Apr 02, 2009 3:47 pm

stifry wrote:You will want to do this in the onCreate(). Android Activity classes don't use constructors to initialize. You might want to check out the Activity lifecycle:

Activity Lifecycle

onCreate() Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.

Always followed by onStart().



Thanx a lot.
your solution worked.
User avatar
totw
Developer
Developer
 
Posts: 27
Joined: Tue Mar 31, 2009 10:35 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 3 guests