08-26-2012 11:20 AM - edited 08-26-2012 11:24 AM
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
08-27-2012 01:52 AM
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());
}
08-27-2012 02:02 AM
1- the database exist when i check
2- that code wouldn't work , i tried but no
Thanks
08-27-2012 03:06 AM - edited 08-27-2012 03:08 AM
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) {
}
}
08-27-2012 08:17 AM
check with following api
http://www.blackberry.com/developers/docs/5.0.0api
08-27-2012 08:30 AM
check with this also
http://www.blackberry.com/developers/docs/7.0.0api
08-27-2012 08:43 AM
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();
08-27-2012 11:45 PM
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.
08-28-2012 06:37 PM
nope
i tried and the exception persist