Java createTableIfNotExists错误-关系已存在
在我的后端:Java createTableIfNotExists错误-关系已存在,java,sqlite,postgresql,ormlite,Java,Sqlite,Postgresql,Ormlite,在我的后端: //create ConnectioSource private static final String DB_NAME = "development"; private ConnectionSource connectionSource; private String databaseUrl = "jdbc:postgresql://localhost:5432/" + DB_NAME; public ConnectionSource getConnectionSource()
//create ConnectioSource
private static final String DB_NAME = "development";
private ConnectionSource connectionSource;
private String databaseUrl = "jdbc:postgresql://localhost:5432/" + DB_NAME;
public ConnectionSource getConnectionSource() {
try {
Class.forName("org.postgresql.Driver");
connectionSource = new JdbcConnectionSource(databaseUrl, "user", "333444");
return connectionSource;
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
但当我尝试重新启动后端时,我收到错误消息
原因:org.postgresql.util.PSQLException:错误:关系“买方\用户”已存在
当我使用SQLite
db时,方法createTableIfNotExists
工作正常,但当我使用postgres
时会发生什么
更新:
我的解决方案:
List<String> tablesList = new ArrayList<String>();
DatabaseMetaData md = connection.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
tablesList.add(rs.getString(3));
}
if(!tablesList.contains("buyer_users"))
TableUtils.createTableIfNotExists(connectionSource, BuyerUser.class);
List tablelist=new ArrayList();
DatabaseMetaData md=connection.getMetaData();
ResultSet rs=md.getTables(null,null,“%”,null);
while(rs.next()){
tableList.add(rs.getString(3));
}
如果(!tableList.contains(“买方用户”))
createTableIfNotExists(connectionSource,BuyerUser.class);
我今天在OrmLite 4.48和PostgreSQL 9.1中遇到了同样的问题
我最后做的是:
// create the table if it does not exist
if(!dao.isTableExists()) {
// use dao.isTableExists() first, as createTableIfNotExists() fails with postgreSQL 9.1 (OrmLite 4.48)
TableUtils.createTableIfNotExists(_pooledConnectionSrc, entityClass);
}
显然,这段代码假设您对创建相应的DAO实例感兴趣。是的,在我的项目中。要创建与表对应的DAO,请参阅类DaoManager中的“createDao”方法
(代码在PgSQL 9.1和MySQL 5.5上都经过测试)您使用的是哪个版本的ormlite?我使用的是4.43 ormlite版本。这个错误在SqlLite上得到了修复,但我想在Postgre4.48版本上没有-没有任何更改。唯一的出路-自定义方法
createTableIfNotExist
…谢谢。这真的很有帮助。我遇到了同样的错误。
// create the table if it does not exist
if(!dao.isTableExists()) {
// use dao.isTableExists() first, as createTableIfNotExists() fails with postgreSQL 9.1 (OrmLite 4.48)
TableUtils.createTableIfNotExists(_pooledConnectionSrc, entityClass);
}