I am using SQLiteConnection to access a sqlite file Firefox generates. My goal is to use this C# console application to find all installed extensions.
Here is the problem, Firefox locks the database on it's first start up, and also whenever I install a new extension. It unlocks the database when I restart Firefox, but I would like to simply not even try to read from the database when it's locked.
As my question asks, is it possible to check if the database is locked?
Here is some code for reading from the database:
if (File.Exists(profilePath))
{
//Connect to the DB.
string connectionPath = @"Data Source=" + profilePath;
using (SQLiteConnection connection = new SQLiteConnection(connectionPath))
{
//Create the command to retrieve the data.
SQLiteCommand command = connection.CreateCommand();
//Open the connection.
try
{
connection.Open();
System.Diagnostics.Debug.WriteLine("Connection Reuslt Code: " + connection.ResultCode());
System.Diagnostics.Debug.WriteLine("Connection State: " + connection.State.ToString());
//Get the tables.
DataTable tables = connection.GetSchema("Tables");
//Prepare the query to retreive all the add ons.
string query = "SELECT name as 'Extension_Name', id as 'extID', updateDate as 'Date' FROM addon";
command.CommandText = query;
command.ExecuteNonQuery();
//Retreive the dataset using the command.
SQLiteDataAdapter da = new SQLiteDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds, "addon");
for (int i = 0; i < ds.Tables["addon"].Rows.Count; i++)
{
//Get the date from the query.
long entryDateTicks = (long)ds.Tables["addon"].Rows[i].ItemArray[2];
//Add the ticks to the unix date. Devide by 1000 to convert ms to seconds.
DateTime unixTime = new DateTime(1970, 1, 1);
DateTime entryDate = unixTime.AddSeconds(entryDateTicks / 1000);
//Add the data to a list/
addonList.Add(new FirefoxAddonEntry(
ds.Tables["addon"].Rows[i].ItemArray[0].ToString()
, ds.Tables["addon"].Rows[i].ItemArray[1].ToString()
, entryDate
));
}
}
catch
{
System.Diagnostics.Debug.WriteLine("Error when opening the firefox sqlite file.");
}
finally
{
connection.Close();
}
}
}
else
{
System.Diagnostics.Debug.WriteLine("The extensions file does not exists.");
}
Transaction looks a follow:
using (TransactionScope tran = new TransactionScope())
{
connection.Open();
System.Diagnostics.Debug.WriteLine("Connection Reuslt Code: " + connection.ResultCode());
System.Diagnostics.Debug.WriteLine("Connection State: " + connection.State.ToString());
//Get the tables.
DataTable tables = connection.GetSchema("Tables");
//Prepare the query to retreive all the add ons.
string query = "SELECT name as 'Extension_Name', id as 'extID', updateDate as 'Date' FROM addon";
command.CommandText = query;
command.ExecuteNonQuery();
//Retreive the dataset using the command.
SQLiteDataAdapter da = new SQLiteDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds, "addon");
for (int i = 0; i < ds.Tables["addon"].Rows.Count; i++)
{
//Get the date from the query.
long entryDateTicks = (long)ds.Tables["addon"].Rows[i].ItemArray[2];
//Add the ticks to the unix date. Devide by 1000 to convert ms to seconds.
DateTime unixTime = new DateTime(1970, 1, 1);
DateTime entryDate = unixTime.AddSeconds(entryDateTicks / 1000);
//Add the data to a list/
addonList.Add(new FirefoxAddonEntry(
ds.Tables["addon"].Rows[i].ItemArray[0].ToString()
, ds.Tables["addon"].Rows[i].ItemArray[1].ToString()
, entryDate
));
}
tran.Complete();
}
Can you try changing your code to code above?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With