简体   繁体   English

如何使用来自 Java 字符串变量的参数在我的 SQLite 数据库中创建一个新表?

[英]How can I create a new table in my SQLite Database using a param from a Java String variable?

I am trying to create a new table in SQLite using a preexisting Java String variable but the variable name is not being recognized as Java... How can I fix this?我正在尝试使用预先存在的 Java 字符串变量在 SQLite 中创建一个新表,但变量名未被识别为 Java ......我该如何解决这个问题?

    String name1 = textViewPerson1.getText().toString();
    String name2 = textViewPerson2.getText().toString();
    String name3 = textViewPerson3.getText().toString();

    try{
        SQLiteDatabase database = this.openOrCreateDatabase("NewUsers",MODE_PRIVATE,null);
        database.execSQL("CREATE TABLE IF NOT EXISTS newUsers (name VARCHAR, hitCount INT(2))");
        database.execSQL("INSERT INTO newusers (name,age) VALUES (name1,17)");
        database.execSQL("INSERT INTO newusers (name,age) VALUES (name2, 16)");
    }catch (Exception e){
        e.printStackTrace();
    }

}

I recommend to you use quotes in the sql sentences and specify the value for your varchar, like :我建议您在 sql 语句中使用引号并为您的 varchar 指定值,例如:

database.execSQL("CREATE TABLE IF NOT EXISTS 'newUsers' ('name' VARCHAR(25), 'hitCount' INT(2))");

And you should exit the "String" Context in the insert lines, something like this :您应该在插入行中退出“字符串”上下文,如下所示:

database.execSQL("INSERT INTO 'newusers' (name,age) VALUES ('"+name1+"',17)");

Obviously there exist more cleans options like StringBuilder, but this should help.显然存在更多像 StringBuilder 这样的清理选项,但这应该会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用字符串变量通过Java的jdbc从SQLite数据库中检索行? - How do I retrieve a row from a SQLite database through Java's jdbc using a string variable? 从Eclipse中的sqlite数据库在JAVA GUI中加载表时如何添加表标题? - How do i add table heading while loading a table in my JAVA GUI from sqlite database in eclipse? 如何在Java中使数据库具有可变列的表? - How I can make a table in database having variable columns in Java? 为什么我不能使用SQLITE在数据库中插入新数据? - Why I can't insert new data in my database using SQLITE? 如何从数据库(java)读取的字符串中为实体设置枚举属性? - How can I set an enum attribute in my entity from a string read from a database (java)? 在java中,如何删除sqlite表? - In java, how can I delete a sqlite table? 如何从4个Excel工作表中的Java数据中创建单个数据库表? - How can i create a single database table in java data from 4 excel sheets? 如何创建一个动态和可重用的方法来创建一个使用java和sqlite的表 - How can i create a dynamic and reusable method to create a table with java and sqlite (使用Spring、MVC、Java、MYSQL)如何让用户通过UI在MYSQL数据库中新建表? - (Using Spring, MVC, Java, MYSQL) How do I let the user create a new table in the MYSQL database through the UI? 使用LiveCycle如何通过按下按钮来使用其他字段中的值创建并填充表格中的新行? - Using LiveCycle How can I create and populate a new row in a table with values from other fields with a button press?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM