Cannot create Database

Put your problem here if it does not fit any of the other categories.

Cannot create Database

Postby erbedo » Tue Mar 18, 2008 12:10 pm

Hi
I'm trying to create a database from an outer class tht my main Activity. The problem is that I always got that my database is invalid, and so I can't work on it.

I built a stand-alone class, with (at the moment) just 3 methods, one for creating the db, one for creating the (only) table, and another one for inserting data into it.

But, as I said, I can't create the db. I looked too in the emulator filesystem, and in fact there's no db under my package.

Where I'm wrong?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.tvfinder.db;
  2.  
  3.  
  4.  
  5. import android.database.Cursor;
  6.  
  7. import android.database.sqlite.SQLiteDatabase;
  8.  
  9. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  10.  
  11. import android.util.Log;
  12.  
  13.  
  14.  
  15. public class DBHandler {
  16.  
  17.  
  18.  
  19.         private static final int DATABASE_VERSION = 1;
  20.  
  21.         private final String MY_DATABASE = "TvDb.db";
  22.  
  23.         private static final String MY_TABLE = "t_programs";
  24.  
  25.         private CursorFactory c = null;
  26.  
  27.         SQLiteDatabase myDB = null;
  28.  
  29.  
  30.  
  31.         public void createDatabase() {
  32.  
  33.                
  34.  
  35.                 try {
  36.  
  37.                         /* Create the Database (no Errors if it already exists) */
  38.  
  39.                         Log.v("CREATING DATABASE","HELLO!!!!");
  40.  
  41.                         myDB = SQLiteDatabase.create(MY_DATABASE, DATABASE_VERSION, c);
  42.  
  43.                 } catch (Exception e) {}
  44.  
  45.                
  46.  
  47.                 /* Open the DB and remember it */
  48.  
  49.                 myDB = SQLiteDatabase.open(MY_DATABASE, c);
  50.  
  51.         }
  52.  
  53.  
  54.  
  55.         public void createTable() {
  56.  
  57.                 createDatabase();
  58.  
  59.                 /* Create a Table in the Database. */
  60.  
  61.                 myDB.execSQL("CREATE TABLE IF NOT EXISTS "
  62.  
  63.                                 + MY_TABLE
  64.  
  65.                                 + " (Title VARCHAR, "
  66.  
  67.                                 + " Channel VARCHAR "
  68.  
  69.                                 + " Date VARCHAR "
  70.  
  71.                                 + " Genre VARCHAR, "
  72.  
  73.                                 + " Description VARCHAR "
  74.  
  75.                                 + " Start VARCHAR );");
  76.  
  77.         }
  78.  
  79.        
  80.  
  81.         public void insertProgram(String Title, String Channel, String Date, String Genre,
  82.  
  83.                                                                 String Description, String Start) {
  84.  
  85.                 myDB.execSQL("INSERT INTO " + MY_TABLE + " " + Title + "," + Channel + "," + Date + "," + Genre
  86.  
  87.                                                 + "," + Description + "," + Start +")");
  88.  
  89.         }
  90.  
  91.  
  92.  
  93.         public Cursor queryByTitle(String titleToQuery) {
  94.  
  95.                 return myDB.query(false, MY_TABLE, null, " Title like '%" + titleToQuery + "%'", null, null, null, null);
  96.  
  97.         }
  98.  
  99.        
  100.  
  101.         public void closeDb() {
  102.  
  103.                 if (myDB != null)
  104.  
  105.                         myDB.close();
  106.  
  107.         }
  108.  
  109. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
Proudly supporting Android :)
erbedo
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu Mar 13, 2008 9:39 pm

Top

Postby Jesmiatka » Tue Mar 18, 2008 1:44 pm

Hi,

you are not using the context, what I think is needed.

This is my constructor of my class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                
  2.  
  3. private final Context                           ctx;
  4.  
  5. private SQLiteDatabase                          database                                = null;
  6.  
  7.  
  8.  
  9. public constructor (Context context){
  10.  
  11.                 ctx = context;
  12.  
  13.                
  14.  
  15.                 try {
  16.  
  17.                         database = ctx.openDatabase(DATABASE_NAME, null);
  18.  
  19.                         Log.i(LOG_TAG, "Database opened");
  20.  
  21.                 } catch (FileNotFoundException e) {
  22.  
  23.                         try {
  24.  
  25.                                 database = ctx.createDatabase(DATABASE_NAME, 1, 0, null);
  26.  
  27.                                 database.execSQL(DATABASE_CREATE);
  28.  
  29.                                 Log.i(LOG_TAG, "Database created");
  30.  
  31.                         } catch (FileNotFoundException e1) {
  32.  
  33.                                 throw new SQLException("Could not create database");
  34.  
  35.                         }
  36.  
  37.                 }
  38.  
  39. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby erbedo » Tue Mar 18, 2008 2:57 pm

Jesmiatka wrote:Hi,

you are not using the context, what I think is needed.

This is my constructor of my class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                
  2. private final Context                           ctx;
  3. private SQLiteDatabase                          database                                = null;
  4.  
  5. public constructor (Context context){
  6.                 ctx = context;
  7.                
  8.                 try {
  9.                         database = ctx.openDatabase(DATABASE_NAME, null);
  10.                         Log.i(LOG_TAG, "Database opened");
  11.                 } catch (FileNotFoundException e) {
  12.                         try {
  13.                                 database = ctx.createDatabase(DATABASE_NAME, 1, 0, null);
  14.                                 database.execSQL(DATABASE_CREATE);
  15.                                 Log.i(LOG_TAG, "Database created");
  16.                         } catch (FileNotFoundException e1) {
  17.                                 throw new SQLException("Could not create database");
  18.                         }
  19.                 }
  20. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Thank you, that's what was missing
Proudly supporting Android :)
erbedo
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu Mar 13, 2008 9:39 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 18 guests