gpt4 book ai didi

java - Java 编写 SQLite 数据库时遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 07:02:03 25 4
gpt4 key购买 nike

我正在尝试使用 JDBC 库在 java 中创建 SQLite 数据库。我的代码可以很好地创建数据库,但是当涉及到在数据库中创建表时,它实际上并没有创建表。我知道这一点是因为我正在尝试打印记录集,但它没有到达打印语句。我的总体目标是解析 XML,然后将其写入相应的字段以在我的 Android 应用程序中使用。这是我的代码。

import java.io.File;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

import java.sql.*;

public class TestingXML
{

public static void main(String[] args)
{

try
{

Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:productrecalls.db");
Statement stat = conn.createStatement();
stat.executeUpdate("drop table if exists products;");
stat.executeUpdate("create table products (date, brandname, productdescription, reason, company, link);");
PreparedStatement prep = conn.prepareStatement("insert into products values (?, ?, ?, ?, ?, ?);");

File fXmlFile = new File("/Users/Trevor/Desktop/1.7-RecallsDataSet.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();

NodeList nList = doc.getElementsByTagName("PRODUCT");

for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);

if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;

String day = eElement.getElementsByTagName("DATE").item(0).getTextContent().substring(5, 7);
String month = eElement.getElementsByTagName("DATE").item(0).getTextContent().substring(8, 11);
String year = eElement.getElementsByTagName("DATE").item(0).getTextContent().substring(12, 17);

String date = month + "-" + day + "-" + year;
String brandname = eElement.getElementsByTagName("BRAND_NAME").item(0).getTextContent();
String productdescription = eElement.getElementsByTagName("PRODUCT_DESCRIPTION").item(0).getTextContent();
String reason = eElement.getElementsByTagName("REASON").item(0).getTextContent();
String company = eElement.getElementsByTagName("COMPANY").item(0).getTextContent();
String link = eElement.getElementsByTagName("COMPANY_RELEASE_LINK").item(0).getTextContent();

prep.setString(1, date);
prep.setString(2, brandname);
prep.setString(3, productdescription);
prep.setString(4, reason);
prep.setString(5, company);
prep.setString(6, link);

conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);

}
}
ResultSet rs = stat.executeQuery("select * from products;");
while (rs.next())
{
System.out.println(" ");
System.out.println("date = " + rs.getString("date"));
System.out.println("brandname = " + rs.getString("brandname"));
System.out.println("productdescription = " + rs.getString("productdescription"));
System.out.println("reason = " + rs.getString("reason"));
System.out.println("company = " + rs.getString("company"));
System.out.println("link = " + rs.getString("link"));
}
}
catch (Exception e)
{
e.printStackTrace();
}

System.out.println("Done.");

}
}

最佳答案

您已经很接近了:)“创建表”语句的语法需要更多信息。这是 SQLite 文档页面: enter link description here

这是我最近在项目中使用的示例语句。它为您提供了创建表语句的具体示例:

create table authors (id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL, first_name varchar(25) NOT NULL,
last_name varchar(25) NOT NULL,created_at datetime, updated_at datetime);

注意!您需要指定列的数据类型。我还向您展示了如何创建主键(其值由数据库生成)。

希望有帮助:)

关于java - Java 编写 SQLite 数据库时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21959453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com