Bug: Temporary table locked
Posted: Thu 15 Apr 2010 10:33
Hi!
Here is the bug related to temporary table locking.
I created one database table (Language) and inserted a few records:
CREATE TABLE Language (
LanguageID varchar(10) PRIMARY KEY NOT NULL,
LanguageName varchar(30) NOT NULL UNIQUE,
IsDefault boolean NOT NULL DEFAULT 0
);
INSERT INTO Language (LanguageID, LanguageName, IsDefault) VALUES ('SR', 'Srpski', '0');
INSERT INTO Language (LanguageID, LanguageName, IsDefault) VALUES ('EN', 'English', '1');
INSERT INTO Language (LanguageID, LanguageName, IsDefault) VALUES ('SV', 'Svenska', '0');
INSERT INTO Language (LanguageID, LanguageName, IsDefault) VALUES ('NO', 'Norsk', '0');
INSERT INTO Language (LanguageID, LanguageName, IsDefault) VALUES ('DE', 'Deutch', '0');
COMMIT;
--------------
Here is the C# code:
const string languageId = "EN";
const string strSql = @"
CREATE TEMPORARY TABLE IF NOT EXISTS TempTbl (
LanguageID varchar(10),
LanguageName varchar(30)
);
insert into TempTbl(LanguageID, LanguageName)
select LanguageID, LanguageName
from Language
order by LanguageName;
select rowid, TempTbl.* from TempTbl order by rowid;
DROP TABLE TempTbl;
";
var dt = new DataTable
{
Locale = System.Globalization.CultureInfo.InvariantCulture
};
using (var sqlConnect = new SQLiteConnection(@"Data Source=D:\Projects\TestDevArtSqlite\DbModel\TestDb.db;FailIfMissing=True"))
{
var command = new SQLiteCommand(strSql, sqlConnect);
var dataAdapt = new SQLiteDataAdapter(command);
dataAdapt.Fill(dt);
}
dataGridResults.DataSource = dt;
--------
I've got an error message: 'database table locked'.
This is because of 'DROP TABLE' statement, but it shouldn't happend.
If I erase the last 'select' statement (select rowid, ...) it will work, but
I'll not receive the data I need.
There is a workaround. If I change sql script in the following way:
const string strSql = @"
DROP TABLE IF EXISTS TempTbl;
CREATE TEMPORARY TABLE IF NOT EXISTS TempTbl (
LanguageID varchar(10),
LanguageName varchar(30)
);
insert into TempTbl(LanguageID, LanguageName)
select LanguageID, LanguageName
from Language
order by LanguageName;
select rowid, TempTbl.* from TempTbl order by rowid;
";
this will work, but it's not good enought because resources are not released. In this case, we have small amount of data in temporary table,
but generally speaking, resources must be released by using drop table at the end of script.
Thank you for your efforts.
Here is the bug related to temporary table locking.
I created one database table (Language) and inserted a few records:
CREATE TABLE Language (
LanguageID varchar(10) PRIMARY KEY NOT NULL,
LanguageName varchar(30) NOT NULL UNIQUE,
IsDefault boolean NOT NULL DEFAULT 0
);
INSERT INTO Language (LanguageID, LanguageName, IsDefault) VALUES ('SR', 'Srpski', '0');
INSERT INTO Language (LanguageID, LanguageName, IsDefault) VALUES ('EN', 'English', '1');
INSERT INTO Language (LanguageID, LanguageName, IsDefault) VALUES ('SV', 'Svenska', '0');
INSERT INTO Language (LanguageID, LanguageName, IsDefault) VALUES ('NO', 'Norsk', '0');
INSERT INTO Language (LanguageID, LanguageName, IsDefault) VALUES ('DE', 'Deutch', '0');
COMMIT;
--------------
Here is the C# code:
const string languageId = "EN";
const string strSql = @"
CREATE TEMPORARY TABLE IF NOT EXISTS TempTbl (
LanguageID varchar(10),
LanguageName varchar(30)
);
insert into TempTbl(LanguageID, LanguageName)
select LanguageID, LanguageName
from Language
order by LanguageName;
select rowid, TempTbl.* from TempTbl order by rowid;
DROP TABLE TempTbl;
";
var dt = new DataTable
{
Locale = System.Globalization.CultureInfo.InvariantCulture
};
using (var sqlConnect = new SQLiteConnection(@"Data Source=D:\Projects\TestDevArtSqlite\DbModel\TestDb.db;FailIfMissing=True"))
{
var command = new SQLiteCommand(strSql, sqlConnect);
var dataAdapt = new SQLiteDataAdapter(command);
dataAdapt.Fill(dt);
}
dataGridResults.DataSource = dt;
--------
I've got an error message: 'database table locked'.
This is because of 'DROP TABLE' statement, but it shouldn't happend.
If I erase the last 'select' statement (select rowid, ...) it will work, but
I'll not receive the data I need.
There is a workaround. If I change sql script in the following way:
const string strSql = @"
DROP TABLE IF EXISTS TempTbl;
CREATE TEMPORARY TABLE IF NOT EXISTS TempTbl (
LanguageID varchar(10),
LanguageName varchar(30)
);
insert into TempTbl(LanguageID, LanguageName)
select LanguageID, LanguageName
from Language
order by LanguageName;
select rowid, TempTbl.* from TempTbl order by rowid;
";
this will work, but it's not good enought because resources are not released. In this case, we have small amount of data in temporary table,
but generally speaking, resources must be released by using drop table at the end of script.
Thank you for your efforts.