Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How to create and open an SQLite database? (Based on NotePad tutorial)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
AJ  
View profile  
 More options May 5 2009, 2:29 pm
From: AJ <warhawk...@charter.net>
Date: Tue, 5 May 2009 11:29:30 -0700 (PDT)
Local: Tues, May 5 2009 2:29 pm
Subject: How to create and open an SQLite database? (Based on NotePad tutorial)
Hello - our dev team is looking into utilizing an SQLite DB as a means
to store data within our application.  We've been following the
official dev guide's NotePad tutorial (http://d.android.com/guide/
tutorials/notepad/notepad-ex1.html) to accomplish this, but we're
stuck on Step 2: the preliminary creation of the database itself.  We
aren't even concerned with inserting into and retrieving from the DB
yet.

Basically, we re-purposed the NoteDbAdapter.java file (renamed to
simply DbAdapter.java) to work with our main activity, called
LifeVault.  When an instance of the DbAdapter class created within
LifeVault.java calls the "open()" function within DbAdapter.java,
however, the Android emulator just crashes and displays a message
along the lines of "the process has stopped working and needs to
close."

The code doesn't have any compilation errors, and it's based on the
code from the official dev guide, so we're pretty perplexed as to why
this happens.

/******************
DbAdapter.java
*******************/
package com.android.lifevault;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbAdapter {

   private static final String TAG = "DbAdapter";
   private DatabaseHelper mDbHelper;
   private SQLiteDatabase mDb;

   private static final String DATABASE_NAME = "FVDB";
   private static final String DATABASE_TABLE_USER = "User";
   private static final String DATABASE_TABLE_ACH = "Ach";
   private static final String DATABASE_TABLE_USER_ACH = "User_Ach";
   private static final int DATABASE_VERSION = 1;

   private final Context mCtx;

   private static class DatabaseHelper extends SQLiteOpenHelper {

       DatabaseHelper(Context context) {
           super(context, DATABASE_NAME, null, DATABASE_VERSION);
       }

       @Override
       public void onCreate(SQLiteDatabase db) {
           // Our "db.execSQL" statements for creating the three tables
live here
       }

       @Override
       public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
           Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
                   + newVersion + ", which will destroy all old
data");
           db.execSQL("DROP TABLE IF EXISTS User_Ach");
           db.execSQL("DROP TABLE IF EXISTS Ach");
           db.execSQL("DROP TABLE IF EXISTS User");
           onCreate(db);
       }
   }

   /**
    * Constructor - takes the context to allow the database to be
    * opened/created
    */
   public DbAdapter(Context ctx) {
       this.mCtx = ctx;
   }

   /**
    ** Open the database - Here's where things get screwy.
    **/
   public DbAdapter open() throws SQLException {
       mDbHelper = new DatabaseHelper(mCtx);
       mDb = mDbHelper.getWritableDatabase();
       return this;
   }

   public void close() {
       mDbHelper.close();
   }

}

/******************
END DbAdapter.java
******************/

/******************
LifeVault.java
******************/
package com.android.lifevault;

import com.android.lifevault.DbAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class LifeVault extends Activity {

        private DbAdapter mDbHelper;

        @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        mDbHelper = new DbAdapter(this);
        mDbHelper.open();   // <--------- Program crashes here
        setContentView(R.layout.main);

        /**The rest of the code**/

     }

}

/********************
END LifeVault.java
********************/

- Am I using the Context class incorrectly?
- Am I missing a library that the app needs to recognize the
getWritableDatabase() funciton?

Those are my two best guesses.  I'm so close I can taste it!
Hopefully someone can help me see this answer that's probably right in
front of my face.  Thank you; the help is greatly appreciated.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
swarup  
View profile  
 More options May 6 2009, 4:37 am
From: swarup <me.s...@gmail.com>
Date: Wed, 6 May 2009 01:37:47 -0700 (PDT)
Local: Wed, May 6 2009 4:37 am
Subject: Re: How to create and open an SQLite database? (Based on NotePad tutorial)
check your table creation statements within onCreate()

On May 5, 11:29 pm, AJ <warhawk...@charter.net> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
AJ  
View profile  
 More options May 8 2009, 12:58 am
From: AJ <warhawk...@charter.net>
Date: Thu, 7 May 2009 21:58:55 -0700 (PDT)
Local: Fri, May 8 2009 12:58 am
Subject: Re: How to create and open an SQLite database? (Based on NotePad tutorial)
Here's the full onCreate() function.  Seems like standard SQL syntax
to me?

**********
public void onCreate(SQLiteDatabase db) {
           db.execSQL("CREATE TABLE IF NOT EXISTS "
                                + DATABASE_TABLE_USER
                                + " (userID INT PRIMARY KEY, name VARCHAR(40),"
                                + " birthdate DATETIME,"
                                + " weight INT(3), heightInches INT, "
                                + " smoking BOOLEAN DEFAULT 'FALSE',"
                                + " school BOOLEAN DEFAULT 'FALSE', "
                                + " reading BOOLEAN DEFAULT 'FALSE',"
                                + " religion BOOLEAN DEFAULT 'FALSE',"
                                + " dating BOOLEAN DEFAULT 'FALSE');");

                        db.execSQL("CREATE TABLE IF NOT EXISTS "
                                + DATABASE_TABLE_ACH
                                + " (achID INT PRIMARY KEY AUTO-INCREMENT, "
                                + " category VARCHAR(40),"
                                + " subCategory VARCHAR(40),"
                                + " icon VARCHAR(40), "
                                + " name VARCHAR(40), "
                                + " description VARCHAR(200),"
                                + " hasProgress BOOLEAN, "
                                + " goal INT,"
                                + " points INT);");

                        db.execSQL("CREATE TABLE IF NOT EXISTS "
                                + DATABASE_TABLE_USER_ACH
                                + " (achID INT PRIMARY KEY, "
                                + " hasProgress BOOLEAN, "
                                + " goal INT, "
                                + " progress INT, "
                                + " isCompleted BOOLEAN, "
                                + " dateCompleted DATETIME, "
                                + " CONSTRAINT FK_achID FOREIGN KEY (achID) REFERENCES Ach, "
                                + " CONSTRAINT FK_hasProgress FOREIGN KEY (hasProgress)
REFERENCES Ach, "
                                + " CONSTRAINT FK_goal FOREIGN KEY (goal) REFERENCES Ach);");
       }
**********

On May 6, 3:37 am, swarup <me.s...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Murphy  
View profile  
 More options May 8 2009, 8:23 am
From: Mark Murphy <mmur...@commonsware.com>
Date: Fri, 08 May 2009 08:23:35 -0400
Local: Fri, May 8 2009 8:23 am
Subject: Re: [android-beginners] Re: How to create and open an SQLite database? (Based on NotePad tutorial)

AJ wrote:
> Here's the full onCreate() function.  Seems like standard SQL syntax
> to me?

What was the error you got? You can get the error log from adb logcat or
DDMS. It should tell you what SQLite or Android did not like.

> public void onCreate(SQLiteDatabase db) {
>               db.execSQL("CREATE TABLE IF NOT EXISTS "
>                                    + DATABASE_TABLE_USER
>                                    + " (userID INT PRIMARY KEY, name VARCHAR(40),"

There is no VARCHAR in SQLite.

http://www.sqlite.org/datatype3.html

>                                    + " birthdate DATETIME,"

There is no DATETIME in SQLite.

>                                    + " weight INT(3), heightInches INT, "
>                                    + " smoking BOOLEAN DEFAULT 'FALSE',"
>                                    + " school BOOLEAN DEFAULT 'FALSE', "
>                                    + " reading BOOLEAN DEFAULT 'FALSE',"
>                                    + " religion BOOLEAN DEFAULT 'FALSE',"
>                                    + " dating BOOLEAN DEFAULT 'FALSE');");

There is no BOOLEAN in SQLite.

>                            db.execSQL("CREATE TABLE IF NOT EXISTS "
>                                    + DATABASE_TABLE_ACH
>                                    + " (achID INT PRIMARY KEY AUTO-INCREMENT, "

AUTOINCREMENT has no hyphen in SQLite.

http://www.sqlite.org/lang_createtable.html

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Android App Developer Training: http://commonsware.com/training.html


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
AJ  
View profile  
 More options May 9 2009, 4:20 pm
From: AJ <warhawk...@charter.net>
Date: Sat, 9 May 2009 13:20:21 -0700 (PDT)
Local: Sat, May 9 2009 4:20 pm
Subject: Re: How to create and open an SQLite database? (Based on NotePad tutorial)
D'oh!  Wow, can't believe I missed that... my first time using SQLite
here, in case that wasn't already obvious.  :p

The database gets created now.  Thank you very much!  A few follow-up
questions:

- Where do these error logs you speak of live?  What exactly are adb
logcat and DDMS?
- How does a DATE datatype get converted from MySql into SQLite?
Text?  Blob?
- sqlite.org is down at the moment, so I'll search around for other
examples, but what are the main differences between MySQL and SQLite
regarding INSERT statements?

You're awesome - thanks again!

On May 8, 7:23 am, Mark Murphy <mmur...@commonsware.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Murphy  
View profile  
 More options May 9 2009, 4:32 pm
From: Mark Murphy <mmur...@commonsware.com>
Date: Sat, 09 May 2009 16:32:33 -0400
Local: Sat, May 9 2009 4:32 pm
Subject: Re: [android-beginners] Re: How to create and open an SQLite database? (Based on NotePad tutorial)

AJ wrote:
> D'oh!  Wow, can't believe I missed that... my first time using SQLite
> here, in case that wasn't already obvious.  :p

It's close enough to the SQL used in other places that you sometimes
forget the idiosyncrasies.

> - Where do these error logs you speak of live?  What exactly are adb
> logcat and DDMS?

http://developer.android.com/guide/developing/tools/ddms.html
http://developer.android.com/guide/developing/tools/adb.html

> - How does a DATE datatype get converted from MySql into SQLite?
> Text?  Blob?

I can't speak for MySQL. With respect to SQLite, I've seen both string
representations (e.g., ISO 8601) and integer representations (Unix
epoch/seconds-since-1970).

> - sqlite.org is down at the moment

It's working for me here.

> so I'll search around for other
> examples, but what are the main differences between MySQL and SQLite
> regarding INSERT statements?

I haven't played much with MySQL in ages. INSERT is pretty close to ANSI
SQL, though, IIRC.

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Android Development Wiki: http://wiki.andmob.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
senthil kumar  
View profile  
 More options May 9 2009, 4:34 pm
From: senthil kumar <cend...@gmail.com>
Date: Sun, 10 May 2009 02:04:09 +0530
Local: Sat, May 9 2009 4:34 pm
Subject: Re: [android-beginners] Re: How to create and open an SQLite database? (Based on NotePad tutorial)

http://www.AWSurveys.com/HomeMain.cfm?RefID=pikaaso<http://www.awsurveys.com/HomeMain.cfm?RefID=pikaaso>

The link above is quite useful......nothing to lose .......zero investment
.......and spending a little time you can start earning  atleast 25 dollars
easily to begin with and thereafter that its upto your efforts. Best to try.
cheeeeeers

   Sam


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »