QSqlQuery Selects Deleted Table - sql-server

I have a QT application and I am showing the user some data from ms sql database which is related to selected ID from comboBox object. My problem is, I've deleted all my tables on my ms sql server but my qt application still finds tables. Here is my sample code :
QString query = 'SELECT Date FROM [dbo].[Parametre] WHERE id=';
QString selected_item = this->ui->comboBox->currentText();
query.append(selected_item);
QSqlQuery qry;
if(qry.exec(query))
{
this->listWidget->addItem->qry.value(0).toString();
}
else
{
qDebug()<<qry.lastError();
}
There's no valid table in my ms sql database but it still writes deleted Date's on my listWidget object. My question is what causes this problem and how can I solve this?

I didn't close my database connection on my previous version on my application. I changed this and now I am closing my connection via exit button and my problem solved. For those have same problem try to close your database connection and open new connection everytime you run your application. If you did not do this qt forces sql to close connection and this causes some problems.

Related

Add new record to ODBC linked table in Access using VBA

I am trying to add new records to a ODBC linked table in VBA Access. When the table was local, stored within the Access database, there was no issue. But when I uploaded the table to our Azure SQL server and linked it to the Access database the code no longer works. The code is on the "onclick" event of a button.
The code is used to add a quick record of when a user logs in to the Access application. I've linked the other server tables for forms and reports of the database, no problems. But have never had to add records in VBA before. The reason it needs to be done in VBA is because it needs to be hidden from the user.
Dim Rst As Recordset
Set Rst = CurrentDb.OpenRecordset("tblLoginLog", dbOpenDynaset)
With Rst
.AddNew
!User = ID
!LoginDate = Now()
.Update
End With
tblLoginLog is the name of the table,
User is a number field for the employee number,
LoginDate is a date field for when login is successful.
If the table is stored locally in the database, the code works, but when the table is moved to the server nothing happens. When I click on the button, literally nothing happens. No error messages, nothing, and when I open the table, no data has been passed to the table.

Delphi Firedac SQL Server: no error raised when update fails because record is locked

I'm currently working on a project to migrate code base from using Advantage Database Server to SQL Server.
I'm using Firedac of XE8 linked to a Microsoft SQL Server 2014 Express.
I have a small test project. There's a TDBGrid showing the content of a table (the query lock mode is Pessimistic, lockpoint immediate).
I have another TQuery with a SQL command like this:
update myTable
set firstName = 'John'
where id = 1
What I do :
I put the first row in Edit mode (by writing something in the cell)
When I press a button, it runs executeSQL on the Update query
Nothing happens -- the update query does not go through
That's fine ... but I was expecting an error message telling me the UPDATE didn't go trough...
How can I get the same behavior but with an error message triggered ?
Essential connection settings to work with row locks :
TFDConnection.UpdateOptions.Lockmode := lmPessimistic;
TFDConnection.UpdateOptions.LockPoint := lmImmediate;
TFDConnection.UpdateOptions.LockWait := False;
Behaviour described is SQL Server waiting for the lock to be removed to finish commiting the UPDATE. By setting your FireDACconnection to 'no wait' it's going to raise an exception as soon as you attempt to do something on the row you've locked by putting the dataset in Edit. Then you can catch this exception to do what you want.

how can I get pyodbc to perform a "SELECT ... INTO" statement without locking?

I'm trying to copy a table in SQL Server, but a simple statement seems to be locking my database when using pyodbc. Here's the code I'm trying:
dbCxn = db.connect(cxnString)
dbCursor = dbCxn.cursor()
query = """\
SELECT TOP(10) *
INTO production_data_adjusted
FROM production_data
"""
dbCursor.execute(query)
The last statement returns immediately, but both LINQPad and SQL Server Management Studio are locked out of the database afterwards (I try to refresh their table lists). Running sp_who2 shows that LINQPad/SSMS are stuck waiting for my pyodbc process. Other databases on the server seem fine, but all access to this database gets held up. The only way I can get these other applications to resolve their stalls is by closing the pyodbc database connection:
dbCxn.close()
This exact same SELECT ... INTO statement statement works fine and takes only a second from LINQPad and SSMS. The above code works fine and doesn't lock the database if I remove the INTO line. It even returns results if I add fetchone() or fetchall().
Can anyone tell me what I'm doing wrong here?
Call the commit function of either the cursor or connection after SELECT ... INTO is executed, for example:
...
dbCursor.execute(query)
dbCursor.commit()
Alternatively, automatic commit of transactions can be specified when the connection is created using autocommit. Note that autocommit is an argument to the connect function, not a connection string attribute, for example:
...
dbCxn = db.connect(cxnString, autocommit=True)
...

In SQL Server (2012), when can I just use a table name "Foo" w/o the full qualification dbname.dbo.Foo?

Is there a setting in the management studio that would allow me, when I create a new query, to use just:
SELECT * FROM Foo
instead of
SELECT * FROM dbname.dbo.Foo
assuming of course there is no ambiguity?
Currently I get an error message. Thanks.
You can set a default schema for a User in SQL Server 2012.
Here is the MSDN page
Note: You can't change the schema after setting it once.
SELECT YOUR DATABASE NAME HERE FROM THE DROP DOWN LIST and then if there is any ambiguity in column names just use two part name i.e TABLENAME.ColumnName
WHere you open a new query window it opens it in Master database context, And people who has been working with sql server for years and years makes this mistake quite often of openning a query window and start executing a script in master db. so your not the only one :)
You can also use the USE statement i.e
USE DataBase_Name
GO
//your query.........

Error in create database postgresql

Is any way to check a database is already exists or not in PostgreSQL?
Iam a new one in PostgreSQL.
And also need to check it from my java application via jdbc driver.
There is no IF NOT EXISTS option for CREATE DATABASE in PostgreSQL. See CREATE DATABASE.
You can check pg_database to see if the DB exists, and only attempt to create it if it does not.
You would need to do this via dblink, because of the limitation Frank points out in the comments:
regress=> SELECT create_database_if_not_exists('test');
ERROR: CREATE DATABASE cannot be executed from a function or multi-command string
CONTEXT: SQL statement "CREATE DATABASE test"
PL/pgSQL function create_database_if_not_exists(text) line 6 at EXECUTE statement
I found an alternate solution for this problem. By using the following query :
ResultSet res = st.executeQuery("select count(*) from pg_catalog.pg_database where datname = 'sample'") ;
res.next();
int count = res.getInt("count");
System.out.println("Count : " + count);
if(count == 0) {
st.executeUpdate("CREATE DATABASE sample");
}
Its work fine
Provided you do not have many databases made in postgreSQL
Here is a simple way to see what databases you have and do not have
Can use pgadmin ,and expand database column and see what databases you have and dont have.
As shown by the yellow box, the databases created in pgadmin, by me, can try this

Resources