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
Contributor
Luis_Pena_Nunez
Posts: 18
Registered: ‎08-23-2012
My Device: Blackberry 9990

Exception thrown in sqlite when i try to do a insert into

[ Edited ]

Hello

 i'm trying to do an insert but always trown an exception

my code is this:

 

URI uri = URI.create("file:///SDCard/databaseApp.db");
db = DatabaseFactory.open(uri);
Statement statement = db
.createStatement("INSERT INTO People('user','pass') "
+ "VALUES ('practice','0020')");
statement.prepare();
statement.close();
db.close();

 

And the other class to create the database and table is this: 

 

URI myURI = URI.create("file:///SDCard/databaseApp.db");
Database db = DatabaseFactory.create(myURI);
db.createStatement("CREATE TABLE IF NOT EXIST People ( "
+ "'user' TEXT, " + "'pass' TEXT );");
ManagerDatabase database = new ManagerDatabase(db);
db.close();
FileConnection fileConnection = (FileConnection) Connector
.open("file:///SDCard/databaseApp.db");
Autentificacion autentificacion = new Autentificacion(db);
pushScreen(autentificacion);

 

The exception is this:
 net.rim.device.api.database.DatabaseException: INSERT INTO People('user','pass') Values (?,?): SQL logic error or missing database
 

 What's the problem? ,i tried in thousand of time and i changed my code thousand of times and the error persist

 

Thanks

Please use plain text.
Developer
kamal_nigam
Posts: 434
Registered: ‎07-23-2012
My Device: Blackberry 10

Re: Exception thrown in sqlite when i try to do a insert into

  1. Check do you have database. Check for databaseApp.db file in you SDK card
  2. Take the help from the following code

try

        {

            // Insert a new record in the DirectoryItems table         

            Statement statement = _db.createStatement("INSERT INTO DirectoryItems VALUES(null, ?, ?, ?, ?)");

            statement.prepare();                       

            statement.bind(1, categoryID);

            statement.bind(2, name);

            statement.bind(3, location);

            statement.bind(4, phone);                   

            statement.execute(); 

            statement.close();

       

            // Retrieve the auto-generated ID of the item just added

            id = _db.lastInsertedRowID();           

        }

        catch(DatabaseException dbe)

        {

            SQLiteDemo.errorDialog(dbe.toString());  

        }         

Thanks
-------------------------------------------------------------------------------------
Press the Accept as solution Button when u got the Solution
Press Kudo to say thank to developer.
-------------------------------------------------------------------------------------.
Please use plain text.
Contributor
Luis_Pena_Nunez
Posts: 18
Registered: ‎08-23-2012
My Device: Blackberry 9990

Re: Exception thrown in sqlite when i try to do a insert into

1- the database exist when i check 

 

2- that code wouldn't work , i tried but no

 

 

 

Thanks

Please use plain text.
Developer
Kiran_Snist
Posts: 174
Registered: ‎01-25-2011
My Device: Not Specified

Re: Exception thrown in sqlite when i try to do a insert into

[ Edited ]

Try to clean all the code u have written, its so inproper... I dont understand the reason for closing the database and opening it again just after u close it... I suggest u not to close the db file until u perform all the operations on it.. The below is the sample code to open and insert data into the database and close it.. One complete flow :

Database creation:

public Database createOrOpenDatabase() {
try {
			// Absolute path of Database
			String strDatabasePath = strDatabaseFilePath + "/"
					+ strDatabaseFolderName + "/" + strDatabaseFileName;
			URI oURI = URI.create(strDatabasePath);

			// Check the existence of database
			if (DatabaseFactory.exists(oURI)) {
				oDatabase = DatabaseFactory.open(oURI);
			}
			// Create if not exists
			else {
				DatabaseSecurityOptions dbso = new DatabaseSecurityOptions(true);
				oDatabase = DatabaseFactory.create(oURI, dbso);

			}
		} catch (Exception e) {
}
return oDatabase
}

 Table creation: If oDatabase is not null and 

strCreateStatement = 

public static final String DATABASE_IMAGES_CREATE_TABLE_STATEMENT = "CREATE TABLE IF NOT EXISTS 'SavedImages'( "
+ "'employeeId' TEXT, " + "'dependentId' TEXT, " + "'image' TEXT )";

public boolean createOrOpenTable(String strCreateStatement,
			Database oDatabase) {
		boolean tableCreation = false;
		try {
			Statement oStatement = oDatabase
					.createStatement(strCreateStatement);
			oStatement.prepare();
			oStatement.execute();
			oStatement.close();
			// Table created successfully
			tableCreation = true;
		} catch (Exception e) {
			
		}
		return tableCreation;
	}

 Table insertion: If tableCreation is true

strInsertStatement = 

"INSERT INTO SavedImages (employeeId,dependentId,image)"
+ "VALUES(?,?,?)";

public boolean insertIntoTable( String strEmployeeId, String strDependentId, String strImage,Database oDatabase,
			 String strInsertStatement) {

		boolean tableInsertion = false;
Statement oStatement = oDatabase
						.createStatement(strInsertStatement);
				oStatement.prepare();

				oStatement.bind(1, strEmployeeId);
				oStatement.bind(2, strDependentId);
				oStatement.bind(3, strImage);

				oStatement.execute();
				oStatement.close();
				// Table insertion success
				tableInsertion = true;

return tableInsertion;
	}

 Finally close the db:

public void closeDatabase(Database oDatabase) {

		try {
			// Close database after all actions are performed on database
			oDatabase.close();
		} catch (Exception e) {
			
		}

	}

 

Please use plain text.
Developer
kamal_nigam
Posts: 434
Registered: ‎07-23-2012
My Device: Blackberry 10

Re: Exception thrown in sqlite when i try to do a insert into

check with following api

http://www.blackberry.com/developers/docs/5.0.0api/data_storage-summary.html

 

Thanks
-------------------------------------------------------------------------------------
Press the Accept as solution Button when u got the Solution
Press Kudo to say thank to developer.
-------------------------------------------------------------------------------------.
Please use plain text.
Developer
kamal_nigam
Posts: 434
Registered: ‎07-23-2012
My Device: Blackberry 10

Re: Exception thrown in sqlite when i try to do a insert into

check with this also

http://www.blackberry.com/developers/docs/7.0.0api/net/rim/device/api/database/package-summary.

 

Thanks
-------------------------------------------------------------------------------------
Press the Accept as solution Button when u got the Solution
Press Kudo to say thank to developer.
-------------------------------------------------------------------------------------.
Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: Exception thrown in sqlite when i try to do a insert into

Is it possible that your problem is simply that you are missing some statements?:

 

So this:


Database db = DatabaseFactory.create(myURI);
db.createStatement("CREATE TABLE IF NOT EXIST People ( "
+ "'user' TEXT, " + "'pass' TEXT );");
db.close();

 

should be


Database db = DatabaseFactory.create(myURI);

Statement statement = db
.createStatement("db.createStatement("CREATE TABLE IF NOT EXIST People ( "
+ "'user' TEXT, " + "'pass' TEXT );");

statement.prepare();

statement.execute();

statement.close();

db.close();

Please use plain text.
Contributor
Luis_Pena_Nunez
Posts: 18
Registered: ‎08-23-2012
My Device: Blackberry 9990

Re: Exception thrown in sqlite when i try to do a insert into

Hi peter_strange 
i'm luis ;-)

 

that code you wrote don't work
in a pdf of sqlite tell this:

 

"It's not necessary create a statement if the request wouldn't return"
then i tried with Statement and without statement and the error persist

Sorry for my bad english :-)
Thanks. 

 

 

Please use plain text.
Contributor
Luis_Pena_Nunez
Posts: 18
Registered: ‎08-23-2012
My Device: Blackberry 9990

Re: Exception thrown in sqlite when i try to do a insert into

nope
i tried and the exception persist 

Please use plain text.