Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close in finally #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 77 additions & 71 deletions SqliteDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,22 @@ public void ExecuteNonQuery (string query)
Debug.Log ("ERROR: Can't execute the query, verify DB origin file");
return;
}
try {
this.Open ();
if (!IsConnectionOpen) {
throw new SqliteException ("SQLite database is not open.");
}

this.Open ();
if (!IsConnectionOpen) {
throw new SqliteException ("SQLite database is not open.");
}

IntPtr stmHandle = Prepare (query);
if (sqlite3_step (stmHandle) != SQLITE_DONE) {
throw new SqliteException ("Could not execute SQL statement.");
IntPtr stmHandle = Prepare (query);
if (sqlite3_step (stmHandle) != SQLITE_DONE) {
throw new SqliteException ("Could not execute SQL statement.");
}
Finalize (stmHandle);
} finally {
this.Close ();
}

Finalize (stmHandle);
this.Close ();
}

/// <summary>
Expand All @@ -209,95 +211,99 @@ public DataTable ExecuteQuery (string query)
return null;
}

this.Open ();
if (!IsConnectionOpen) {
throw new SqliteException ("SQLite database is not open.");
}

IntPtr stmHandle = Prepare (query);

int columnCount = sqlite3_column_count (stmHandle);

var dataTable = new DataTable ();
for (int i = 0; i < columnCount; i++) {
string columnName = Marshal.PtrToStringAnsi (sqlite3_column_name (stmHandle, i));
dataTable.Columns.Add (columnName);
}


//populate datatable
while (sqlite3_step(stmHandle) == SQLITE_ROW) {
object[] row = new object[columnCount];
try {
this.Open ();
if (!IsConnectionOpen) {
throw new SqliteException ("SQLite database is not open.");
}

IntPtr stmHandle = Prepare (query);

int columnCount = sqlite3_column_count (stmHandle);


for (int i = 0; i < columnCount; i++) {
switch (sqlite3_column_type (stmHandle, i)) {
case SQLITE_INTEGER:
row [i] = sqlite3_column_int (stmHandle, i);
break;

case SQLITE_TEXT:
IntPtr text = sqlite3_column_text (stmHandle, i);
row [i] = Marshal.PtrToStringAnsi (text);
break;

case SQLITE_FLOAT:
row [i] = sqlite3_column_double (stmHandle, i);
break;

case SQLITE_BLOB:
IntPtr blob = sqlite3_column_blob (stmHandle, i);
int size = sqlite3_column_bytes (stmHandle, i);
byte[] data = new byte[size];
Marshal.Copy (blob, data, 0, size);
row [i] = data;
break;

case SQLITE_NULL:
row [i] = null;
break;
string columnName = Marshal.PtrToStringAnsi (sqlite3_column_name (stmHandle, i));
dataTable.Columns.Add (columnName);
}

//populate datatable
while (sqlite3_step(stmHandle) == SQLITE_ROW) {
object[] row = new object[columnCount];
for (int i = 0; i < columnCount; i++) {
switch (sqlite3_column_type (stmHandle, i)) {
case SQLITE_INTEGER:
row [i] = sqlite3_column_int (stmHandle, i);
break;

case SQLITE_TEXT:
IntPtr text = sqlite3_column_text (stmHandle, i);
row [i] = Marshal.PtrToStringAnsi (text);
break;

case SQLITE_FLOAT:
row [i] = sqlite3_column_double (stmHandle, i);
break;

case SQLITE_BLOB:
IntPtr blob = sqlite3_column_blob (stmHandle, i);
int size = sqlite3_column_bytes (stmHandle, i);
byte[] data = new byte[size];
Marshal.Copy (blob, data, 0, size);
row [i] = data;
break;

case SQLITE_NULL:
row [i] = null;
break;
}
}

dataTable.AddRow (row);
}

dataTable.AddRow (row);

Finalize (stmHandle);
} finally {
this.Close ();
}

Finalize (stmHandle);
this.Close ();
return dataTable;
}
public void ExecuteScript (string script)
{
string[] statements = script.Split (';');
foreach (string statement in statements) {
if (!string.IsNullOrEmpty (statement.Trim ())) {
ExecuteNonQuery (statement);
}
}
}
#endregion
#region Private Methods
#endregion
#region Private Methods
private IntPtr Prepare (string query)
{
IntPtr stmHandle;
if (sqlite3_prepare_v2 (_connection, query, query.Length, out stmHandle, IntPtr.Zero) != SQLITE_OK) {
IntPtr errorMsg = sqlite3_errmsg (_connection);
throw new SqliteException (Marshal.PtrToStringAnsi (errorMsg));
}
return stmHandle;
}
private void Finalize (IntPtr stmHandle)
{
if (sqlite3_finalize (stmHandle) != SQLITE_OK) {
throw new SqliteException ("Could not finalize SQL statement.");
}
}
#endregion
#endregion
}