Find out if database already exists in ORMLite - ormlite

I want to tell ORMLite to create a database if one already doesn't. How can I find out if the database that I want to construct is there. (I'm using H2 DB with it.)

How can I find out if the database that I want to construct is there. (I'm using H2 DB with it.)
This is database dependent. Looks like for H2 you can add something to the database URL. See this URL from their docs:
http://www.h2database.com/html/features.html#database_only_if_exists
To quote:
By default, when an application calls DriverManager.getConnection(url, ...) and the database specified in the URL does not yet exist, a new (empty) database is created. In some situations, it is better to restrict creating new databases, and only allow to open existing databases. To do this, add ;IFEXISTS=TRUE to the database URL. In this case, if the database does not already exist, an exception is thrown when trying to connect. The connection only succeeds when the database already exists.

Related

Linked Databases in SQL Developer

I'm using SQL Developer to host some data, but I think my configuration isn't right. Lets say I have a database named DB1 with the tables
DB1
-db1_name
-db1_cities
The problem is when I try to create a new database let say DB2, SQLdeveloper creates the database but it also includes all the tables from DB1 (db1_name,db1_cities). Also if I removed the tables from DB2, the tables also disappear from DB1. How can I change my configuration so this does not happen anymore and each database is independent from each other?
To create a new data base I usually go in the SQL Developer GUI interface to connections, then I click on new Connection and provide the connection name, user name, and password. I leave the connection type to basic and the role to default. The host name as local host, and the default port.
That's happening because you are currently adding a new database connection instead of creating a database.
To create a new database from scratch, take a look at this link, it'll take you through step by step on creating the new database.

create a database on startup

I am using play 2.1.1.
What I want is, when play starts it should also create a database.
So as said in documentation, in my evolution 1.sql I added the line Create Database videos. But instead I get the below error. (Which is obvious, as it is trying to connect to database which does not exist).
What should I do, so that play on start up, creates a database and then uses it? Currently it first tries to connect to a database and then call my evolution files.
See: where-do-i-put-startup-code-in-play-framework
In there you connect to your mysql with the appropriate user / pw and create a database, you can use DDL for all your tables and predefined data.

Delete database. Entity Framework

I followed the following MDSN tutorial:
http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model
Then I tried this guide according to my needs and I set up a database GAMES.MDF.
Then, I deleted the database and set it up again, is supposedly work (I can write and read data), but there is no such database in the APP_DATA folder. It seems to exist, but somewhere else on my PC.
 
I even tried a new project and it did not work, works but not in the library, and it even uses the data I created before. I even deleted the DB from SQL Server Management Studio 2008.
How do I delete it permanently, not to remain any trace of it?
Check for the connection string in the web.config - You'll probably see the file location there.
Your connection string should ideally name the database you want to use. I would not recommend specifying the MDF file, unless you know you are using SQL Express. Source: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/f21c0728-935d-492a-baaf-ff2704e3683b/attachdbfilename-option-in-connection-string?forum=sqldataaccess0
Instead, this is how it is done on SQL Server:
initial catalog=MyDatabase
To be sure you are accessing the database you think you are, run this SQL query through your app, using your connection string, and look at the physical file location of your MDF file. This query is handy for knowing which DB is tied to which DB files.
select * from [dbo].[database_files]
As far as actually clearing the DB, this article deals with managing database initializers and seeds. You might be experiencing a problem due to that:
How to delete and recreate from scratch an existing EF Code first database
Outside of EF, SQL deletes databases like this:
use master
drop database MyDatabase

libpq code to create, list and delete databases (C++/VC++, PostgreSQL)

I am new to the PostgreSQL database. What my visual c++ application needs to do is to create multiple tables and add/retrieve data from them.
Each session of my application should create a new and distinct database. I can use the current date and time for a unique database name.
There should also be an option to delete all the databases.
I have worked out how to connect to a database, create tables, and add data to tables. I am not sure how to make a new database for each run or how to retrieve number and name of databases if user want to clear all databases.
Please help.
See the libpq examples in the documentation. The example program shows you how to list databases, and in general how to execute commands against the database. The example code there is trivial to adapt to creating and dropping databases.
Creating a database is a simple CREATE DATABASE SQL statement, same as any other libpq operation. You must connect to a temporary database (usually template1) to issue the CREATE DATABASE, then disconnect and make a new connection to the database you just created.
Rather than creating new databases, consider creating new schema instead. Much less hassle, since all you need to do is change the search_path or prefix your table references, you don't have to disconnect and reconnect to change schemas. See the documentation on schemas.
I question the wisdom of your design, though. It is rarely a good idea for applications to be creating and dropping databases (or tables, except temporary tables) as a normal part of their operation. Maybe if you elaborated on why you want to do this, we can come up with solutions that may be easier and/or perform better than your current approach.

web application cannot see tables in new postgresql schema

I moved some tables in my postgresql (8.2) database to a new schema.
at first, my "user" could not see the tables in the new schema, but I used set search_path to tell it to look in this new schema
I access these tables with a simple web application that uses hibernate. At first, my web application, which uses the "user" user, could not see the tables either, even after I set the search_path. I eventually set the default-schema in the hibernate config file and it worked, but I understand from what I've read that I should not have to set this property? I have a few JDBC queries in this app that still can't see the tables in the new schema.
I've browsed through the postgresql docs and can't find the cause of my problems. Is there something simple I'm missing?
SET search_path is not persisted. It is only valid for the current session.
You need to use ALTER USER to make that change permanently, but you don't need special privileges to change the user you are logged in with (i.e. "yourself")