Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
apjustin
Posts: 37
Registered: ‎05-27-2010
My Device: 9700 Storm
Accepted Solution

Checking SQLite Table Existance

Hello All,

 

In my application, I have to check whether a SQLite table is existing or not. If it's not existing, I have to create the table and insert values into it. Otherwise, I need not have to create but insert. Is there any method like DatabaseFactory.exists(URI) to find out whether a table is already created or not?

 

Thanks in advance

--------------------------------------------------------------------------------------
Justin Aloor

Feel free to press the Kudos Button to thank the user who helped you.
Please mark posts as solved if you found a solution.
Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Checking SQLite Table Existance

U can check with the FileConnection object.

 

FileConnection fcon=(FileConnection)Connector.open("sqlitedb.db");

if(fcon.exists())

{

.........

}

------------------------------------------------------------------------------------

Press Kudo to say thank to developer.
Also Press the Accept as solution Button when u got the Solution.

Please use plain text.
Developer
apjustin
Posts: 37
Registered: ‎05-27-2010
My Device: 9700 Storm

Re: Checking SQLite Table Existance

I was not asking how to check the database existance. Please see a sample code

 

Database database;
try{
    URI myURI = URI.create("file:///SDCard/sqlitedb.db");
    if(DatabaseFactory.exists(myURI)){
        database = DatabaseFactory.open(myURI);	
        Statement createStmt = database.createStatement("CREATE TABLE 'ChangeStatus'('WoNo' TEXT, 'ShipName' TEXT, 'ShipCity' TEXT");
        createStmt.prepare();
        createStmt.execute();
        createStmt.close();
        database.close();
    }
			
}catch(Exception e){}

 

We can check whether or not sqlitedb.db is existing using DatabaseFactory.exists() as shown above. I need to check whether "ChangeStatus" table is existing or not. Is there any solution for that?  

--------------------------------------------------------------------------------------
Justin Aloor

Feel free to press the Kudos Button to thank the user who helped you.
Please mark posts as solved if you found a solution.
Please use plain text.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Checking SQLite Table Existance

No.

------------------------------------------------------------------------------------

Press Kudo to say thank to developer.
Also Press the Accept as solution Button when u got the Solution.

Please use plain text.
Developer
peter_strange
Posts: 19,595
Registered: ‎07-14-2008
My Device: Not Specified

Re: Checking SQLite Table Existance

Here is some code that might help:

 

            d = DatabaseFactory.open(myURI);
            String SQLTableSelect = "SELECT name FROM sqlite_master " +
                                    " WHERE type='table' " +
                                   " ORDER BY name";
            Statement st = d.createStatement(SQLTableSelect);
            st.prepare();
            c = st.getCursor();
            Row r;
            while ( c.next() ) {
                r = c.getRow();
                String name = r.getString(0);
                // match your name in here
            }
            c.close();

 

Please use plain text.
New Contributor
jcaruso
Posts: 6
Registered: ‎01-10-2011
My Device: Not Specified

Re: Checking SQLite Table Existance

[ Edited ]

I found this post while looking for help in the PlayBook development forums, so I thought I'd post the following sample just incase anyone there happens to find this post as well.

 

You dont have access to sqlite_master through the Adobe Air api. In order to access table information you need to request the schema from your the SQLConnection object, like so:

 

    import flash.data.SQLConnection;
    import flash.data.SQLSchemaResult;
    import flash.data.SQLTableSchema;
    import flash.filesystem.File;

 

            var dbconn:smileyfrustrated:QLConnection;

            var schema:smileyfrustrated:QLSchemaResult;

            var dbfile:File;

            var blnTableExists:Boolean;

 

           // I extracted this from a function I wrote, so if you do the same just make this variable a parameter for your function.

            var pstrTableName:smileyfrustrated:tring;

            pstrTableName = "table_you_are_looking_for";

 

            // get your database file
            dbfile = File.applicationStorageDirectory.resolvePath("dbname.db");           

 

            // open database file

            dbconn = new SQLConnection();

            dbconn.open(dbfile);

 

            // load schema then get the schema result

            dbconn.loadSchema();
            schema = dbconn.getSchemaResult();

 

            // loop through your schema tables

            if (schema != null)
            {
                for each (var table:smileyfrustrated:QLTableSchema in schema.tables)
                {
                    if (table.name == pstrTableName)
                    {
                        blnTableExists = true;
                        break;
                    }
                }
            }

 

           dbconn.close();

Please use plain text.
New Contributor
vsamurali
Posts: 4
Registered: ‎01-07-2013
My Device: Not specified
My Carrier: Developer

Re: Checking SQLite Table Existance

Hi peter

The above code giving illegal argument exception...
java.lang.IllegalArgumentException : invalid character in name.

Pls, let me know what am making mistake,

thanks in advance.

Please use plain text.