Sqlite 插入在重新连接时不起作用
我的问题是,每当我第一次在 SDCard 中创建数据库并创建一个表并在其中插入一些值时都工作正常。 但只有一次,如果现在我想在现有的数据库中添加更多数据,我正在做的是, 我要去编写插入语句并插入更多值的课程,但是当我运行应用程序并在 SQLite 数据库浏览器中浏览数据库时,它不会偏转任何更改。我看不到我新插入的值
import net.rim.device.api.ui.UiApplication;
public class InsertDataScreen extends UiApplication {
public static void main(String[] args) {
InsertDataScreen theApp = new InsertDataScreen();
theApp.enterEventDispatcher();
}
public InsertDataScreen() {
pushScreen(new CreateDatabaseScreen());
}
}
,我的第二类是我创建数据库并插入值的地方,当我完成此操作时,我用一行创建了数据库 现在我想在我的数据库中添加一行,因为当我完成时我已经评论了插入语句,我期望里面有两行,但它没有改变......
import net.rim.device.api.database.Database;
import net.rim.device.api.database.DatabaseFactory;
import net.rim.device.api.database.Row;
import net.rim.device.api.database.Statement;
import net.rim.device.api.io.URI;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;
public class CreateDatabaseScreen extends MainScreen {
Database d;
public CreateDatabaseScreen(){
LabelField title = new LabelField("SQLite Create database sample and " +
"inserting some value ",
LabelField.ELLIPSIS|LabelField.USE_ALL_WIDTH);
setTitle(title);
add(new RichTextField("Creating a Database and will Add some data" +
"MyTest.db on the SDCard"));
try {
URI uri = URI.create("file:///SDCard/Databases/SQLite_Guide/" +
"MyTestDatabase.db");
d = DatabaseFactory.create(uri);
d.close();
d = DatabaseFactory.open(uri);
Statement s = d.createStatement( "CREATE TABLE 'People' ( " +
"'Name' TEXT, " +
"'Age' INTEGER )");
s.prepare();
s.execute();
s.close();
Statement s1 = d.createStatement("INSERT INTO People(Name,Age) " +
"VALUES ('Uttam',23)");
s1.prepare();
s1.execute();
s1.close();
/**
Statement s2 = d.createStatement("INSERT INTO People(Name,Age) " +
"VALUES ('Rakesh',26)");
s2.prepare();
s2.execute();
s2.close();
*/
d.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
My problem is whenever I first create a database in SDCard and also making a table and inserting some values inside it is working fine .
But only once, if now i want to add some more data in my existed DB What i am doing is ,
I am going to the class where I have written the insert statement and inserting some more values but when i run my application and browsing my database in SQLite database browser it doesnot deflect any changes. I cant see my new inserted values
import net.rim.device.api.ui.UiApplication;
public class InsertDataScreen extends UiApplication {
public static void main(String[] args) {
InsertDataScreen theApp = new InsertDataScreen();
theApp.enterEventDispatcher();
}
public InsertDataScreen() {
pushScreen(new CreateDatabaseScreen());
}
}
and my second class is this where i was creating the database and inserting value when i have done this my database created with one row
now i want to add one more row inside my database for that I have commented the Insert Statement when i done that i was expecting two rows inside but it doesnot changed...
import net.rim.device.api.database.Database;
import net.rim.device.api.database.DatabaseFactory;
import net.rim.device.api.database.Row;
import net.rim.device.api.database.Statement;
import net.rim.device.api.io.URI;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;
public class CreateDatabaseScreen extends MainScreen {
Database d;
public CreateDatabaseScreen(){
LabelField title = new LabelField("SQLite Create database sample and " +
"inserting some value ",
LabelField.ELLIPSIS|LabelField.USE_ALL_WIDTH);
setTitle(title);
add(new RichTextField("Creating a Database and will Add some data" +
"MyTest.db on the SDCard"));
try {
URI uri = URI.create("file:///SDCard/Databases/SQLite_Guide/" +
"MyTestDatabase.db");
d = DatabaseFactory.create(uri);
d.close();
d = DatabaseFactory.open(uri);
Statement s = d.createStatement( "CREATE TABLE 'People' ( " +
"'Name' TEXT, " +
"'Age' INTEGER )");
s.prepare();
s.execute();
s.close();
Statement s1 = d.createStatement("INSERT INTO People(Name,Age) " +
"VALUES ('Uttam',23)");
s1.prepare();
s1.execute();
s1.close();
/**
Statement s2 = d.createStatement("INSERT INTO People(Name,Age) " +
"VALUES ('Rakesh',26)");
s2.prepare();
s2.execute();
s2.close();
*/
d.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一旦数据库创建,我们就不应该再次创建它。
并且也不能一次又一次地创建表。 Create 应该几乎被执行一次。
是的,你应该检查数据库和表是否存在......
如果没有创建数据库和表,如果数据库存在,则插入您想要插入表中的值....
所以代码应该是这样的
Get Solved once a database created we shouldnot create it again.
and also cannot create the table again and again. Create should be executed almost one time.
And yes you should check the database and the table already exist or not ....
if not create the database and table and if database existed then insert the value u want to insert in the table ....
so the code should be like this
你不能每次都盲目地创建表。如果表已经存在,则当您执行()时该语句将抛出异常。您需要检查该表是否已存在,然后添加所需的新列,或者您需要跟踪架构版本,然后使用新代码应用增量架构更新。
如果这只是为了开发,则需要删除现有数据库并重新开始。
You can't just blindly create the table every time. If the table already exists, that statement will throw an exception when you execute(). You need to check that the table already exists, and then add the new column you want, or you need to keep track of a schema version, and then apply the incremental schema updates with the new code.
If this is just for development, you need to delete the existing database and start over.