本文整理汇总了C#中SQLiteConnection.Prepare方法的典型用法代码示例。如果您正苦于以下问题:C# SQLiteConnection.Prepare方法的具体用法?C# SQLiteConnection.Prepare怎么用?C# SQLiteConnection.Prepare使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SQLiteConnection
的用法示例。
在下文中一共展示了SQLiteConnection.Prepare方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestPrepareValidStatement
public void TestPrepareValidStatement()
{
using (var connection = new SQLiteConnection(this.databaseRelativePath))
{
using (var statement = connection.Prepare("CREATE TABLE IF NOT EXISTS t(x INTEGER, y TEXT);"))
{
}
}
}
示例2: TestPrepareInvalidStatement
public void TestPrepareInvalidStatement()
{
Assert.ThrowsException<SQLiteException>(() =>
{
using (var connection = new SQLiteConnection(this.databaseRelativePath))
{
using (var statement = connection.Prepare("AN INVALID STATEMENT;"))
{
}
}
});
}
示例3: TestDataType
public void TestDataType()
{
using (var connection = new SQLiteConnection(this.databaseRelativePath))
{
using (var statement = connection.Prepare("DROP TABLE IF EXISTS TestDataType;"))
{
statement.Step();
}
using (var statement = connection.Prepare("CREATE TABLE TestDataType(id INTEGER, i INTEGER, t TEXT, r REAL, b BLOB, n);"))
{
statement.Step();
}
using (var statement = connection.Prepare("INSERT INTO TestDataType(id, i, t, r, b) VALUES(@id,@i,@t,@r,@b);"))
{
statement.Bind(1, 0);
statement.Bind("@i", this.GetRandomInteger());
statement.Bind(3, this.GetRandomString());
statement.Bind("@r", this.GetRandomReal());
statement.Bind(5, this.GetRandomBlob());
statement.Step();
statement.Reset();
statement.ClearBindings();
}
using (var statement = connection.Prepare("SELECT id, i, t, r, b, n FROM TestDataType ORDER BY id ASC;"))
{
statement.Step();
var integerType = statement.DataType(1);
var textType = statement.DataType(2);
var floatType = statement.DataType(3);
var blobType = statement.DataType(4);
var nullType = statement.DataType(5);
Assert.AreEqual(SQLiteType.INTEGER, integerType);
Assert.AreEqual(SQLiteType.TEXT, textType);
Assert.AreEqual(SQLiteType.FLOAT, floatType);
Assert.AreEqual(SQLiteType.BLOB, blobType);
Assert.AreEqual(SQLiteType.NULL, nullType);
}
using (var statement = connection.Prepare("DROP TABLE TestDataType;"))
{
statement.Step();
}
}
}
示例4: TestLastInsertRowId
public void TestLastInsertRowId()
{
using (var connection = new SQLiteConnection(this.databaseRelativePath))
{
using (var statement = connection.Prepare("DROP TABLE IF EXISTS TestLastInsertedRowId;"))
{
statement.Step();
}
using (var statement = connection.Prepare("CREATE TABLE TestLastInsertedRowId (id INTEGER PRIMARY KEY AUTOINCREMENT, desc TEXT);"))
{
statement.Step();
}
using (var statement = connection.Prepare("INSERT INTO TestLastInsertedRowId (desc) VALUES (@desc);"))
{
statement.Bind("@desc", "Desc 1");
statement.Step();
statement.Reset();
statement.ClearBindings();
}
var lastId = connection.LastInsertRowId();
Assert.AreEqual(1, lastId);
using (var statement = connection.Prepare("DROP TABLE TestLastInsertedRowId;"))
{
statement.Step();
}
}
}
示例5: TestColumnDataCount
public void TestColumnDataCount()
{
using (var connection = new SQLiteConnection(this.databaseRelativePath))
{
using (var statement = connection.Prepare("DROP TABLE IF EXISTS TestColumnDataCount;"))
{
statement.Step();
}
using (var statement = connection.Prepare("CREATE TABLE TestColumnDataCount(id INTEGER, desc TEXT);"))
{
statement.Step();
}
using (var statement = connection.Prepare("INSERT INTO TestColumnDataCount(id, desc) VALUES(@id,@desc);"))
{
statement.Bind(1, 1);
statement.Bind("@desc", "Desc 1");
statement.Step();
statement.Reset();
statement.ClearBindings();
}
using (var statement = connection.Prepare("SELECT id, desc AS desc FROM TestColumnDataCount ORDER BY id ASC;"))
{
var columnCount = statement.ColumnCount;
var dataCount = statement.DataCount;
Assert.AreEqual(2, columnCount);
Assert.AreEqual(0, dataCount);
statement.Step();
columnCount = statement.ColumnCount;
dataCount = statement.DataCount;
Assert.AreEqual(2, columnCount);
Assert.AreEqual(2, dataCount);
statement.Step();
columnCount = statement.ColumnCount;
dataCount = statement.DataCount;
Assert.AreEqual(2, columnCount);
Assert.AreEqual(0, dataCount);
}
using (var statement = connection.Prepare("DROP TABLE TestColumnDataCount;"))
{
statement.Step();
}
}
}
示例6: TestColumnSameName
public void TestColumnSameName()
{
using (var connection = new SQLiteConnection(this.databaseRelativePath))
{
using (var statement = connection.Prepare("DROP TABLE IF EXISTS TestColumnSameName;"))
{
statement.Step();
}
using (var statement = connection.Prepare("CREATE TABLE TestColumnSameName(id INTEGER, desc TEXT);"))
{
statement.Step();
}
using (var statement = connection.Prepare("SELECT id, desc AS descrip, desc, desc, SUM(id), desc \"some name\", desc \"sOmE NaMe\" FROM TestColumnSameName ORDER BY id ASC;"))
{
var col0 = statement.ColumnName(0);
var col1 = statement.ColumnName(1);
var index2 = statement.ColumnIndex("desc");
var col3 = statement.ColumnName(3);
var col4 = statement.ColumnName(4);
var index5 = statement.ColumnIndex("some name");
var index6 = statement.ColumnIndex("sOmE NaMe");
Assert.AreEqual(col0, "id");
Assert.AreEqual(col1, "descrip");
Assert.AreEqual(index2, 2);
Assert.AreEqual(col3, "desc");
Assert.AreEqual(col4, "SUM(id)");
Assert.AreEqual(index5, 5);
Assert.AreEqual(index6, 6);
}
using (var statement = connection.Prepare("DROP TABLE TestColumnSameName;"))
{
statement.Step();
}
}
}
示例7: TestBindPrimitiveTypes
public void TestBindPrimitiveTypes()
{
var numRecords = this.rnd.Next(1, 11);
var insertedRecords = new List<Tuple<int, byte, sbyte, short, ushort, int, uint, Tuple<long, ulong, char, string, decimal, float, double>>>(numRecords);
var queriedRecords = new List<Tuple<int, byte, sbyte, short, ushort, int, uint, Tuple<long, ulong, char, string, decimal, float, double>>>(numRecords);
for (var i = 0; i < numRecords; i++)
{
insertedRecords.Add(new Tuple<int, byte, sbyte, short, ushort, int, uint, Tuple<long, ulong, char, string, decimal, float, double>>(
i,
(byte)this.GetRandomInteger(),
(sbyte)this.GetRandomInteger(),
(short)this.GetRandomInteger(),
(ushort)this.GetRandomInteger(),
(int)this.GetRandomInteger(),
(uint)this.GetRandomInteger(),
new Tuple<long, ulong, char, string, decimal, float, double>(
this.GetRandomInteger(),
(ulong)Math.Abs(this.GetRandomInteger()),
this.GetRandomString()[0],
this.GetRandomString(),
(decimal)this.GetRandomReal(),
(float)this.GetRandomReal(),
this.GetRandomReal())));
}
using (var connection = new SQLiteConnection(this.databaseRelativePath))
{
using (var statement = connection.Prepare("DROP TABLE IF EXISTS TestBindPrimitiveTypes;"))
{
statement.Step();
}
using (var statement = connection.Prepare("CREATE TABLE TestBindPrimitiveTypes(id INTEGER, b INTEGER, sb INTEGER, s INTEGER, us INTEGER, i INTEGER, ui INTEGER, l INTEGER, ul INTEGER, c TEXT, st TEXT, m REAL, f REAL, d REAL);"))
{
statement.Step();
}
using (var statement = connection.Prepare("INSERT INTO TestBindPrimitiveTypes(id, b, sb, s, us, i, ui, l, ul, c, st, m, f, d) VALUES(@id,@b,@sb,@s,@us,@i,@ui,@l,@ul,@c,@st,@m,@f,@d);"))
{
foreach (var record in insertedRecords)
{
statement.Bind("@id", record.Item1);
statement.Bind("@b", record.Item2);
statement.Bind("@sb", record.Item3);
statement.Bind("@s", record.Item4);
statement.Bind("@us", record.Item5);
statement.Bind("@i", record.Item6);
statement.Bind("@ui", record.Item7);
statement.Bind("@l", record.Rest.Item1);
statement.Bind("@ul", record.Rest.Item2);
statement.Bind("@c", record.Rest.Item3);
statement.Bind("@st", record.Rest.Item4);
statement.Bind("@m", record.Rest.Item5);
statement.Bind("@f", record.Rest.Item6);
statement.Bind("@d", record.Rest.Item7);
statement.Step();
statement.Reset();
statement.ClearBindings();
}
}
using (var statement = connection.Prepare("SELECT id, b, sb, s, us, i, ui, l, ul, c, st, m, f, d FROM TestBindPrimitiveTypes ORDER BY id ASC;"))
{
while (statement.Step() == SQLiteResult.ROW)
{
var id = (int)statement.GetInteger("id");
var b = (byte)statement.GetInteger("b");
var sb = (sbyte)statement.GetInteger("sb");
var s = (short)statement.GetInteger("s");
var us = (ushort)statement.GetInteger("us");
var i = (int)statement.GetInteger("i");
var ui = (uint)statement.GetInteger("ui");
var l = statement.GetInteger("l");
var ul = (ulong)statement.GetInteger("ul");
var c = statement.GetText("c")[0];
var st = statement.GetText("st");
var m = (decimal)statement.GetFloat("m");
var f = (float)statement.GetFloat("f");
var d = statement.GetFloat("d");
queriedRecords.Add(new Tuple<int, byte, sbyte, short, ushort, int, uint, Tuple<long, ulong, char, string, decimal, float, double>>(
id,
b,
sb,
s,
us,
i,
ui,
new Tuple<long, ulong, char, string, decimal, float, double>(
l,
ul,
c,
st,
m,
f,
d)));
//.........这里部分代码省略.........
示例8: TestAggregate
public void TestAggregate()
{
using (var connection = new SQLiteConnection(this.databaseRelativePath))
{
connection.CreateAggregate(
"CUSTOMAGGSUM",
1,
new AggregateStep((aggregateContextData, arguments) =>
{
aggregateContextData["Acum"] = aggregateContextData.ContainsKey("Acum") ? (long)arguments[0] + (long)aggregateContextData["Acum"] : (long)arguments[0];
}),
new AggregateFinal((aggregateContextData) =>
{
return aggregateContextData.ContainsKey("Acum") ? (long)aggregateContextData["Acum"] : 0L;
}));
using (var statement = connection.Prepare("DROP TABLE IF EXISTS TestAggregate;"))
{
statement.Step();
}
using (var statement = connection.Prepare("CREATE TABLE TestAggregate(id INTEGER);"))
{
statement.Step();
}
using (var statement = connection.Prepare("INSERT INTO TestAggregate(id) VALUES(@id);"))
{
for (var value = 0; value < 10; value++)
{
statement.Bind(1, value);
statement.Step();
statement.Reset();
statement.ClearBindings();
}
}
using (var statement = connection.Prepare("SELECT CUSTOMAGGSUM(id) AS CustomResult FROM TestAggregate;"))
{
var rowTotal = 0;
while (statement.Step() == SQLiteResult.ROW)
{
rowTotal++;
var totalSum = (long)statement[0];
Assert.AreEqual(45, totalSum);
}
Assert.AreEqual(1, rowTotal);
}
using (var statement = connection.Prepare("DROP TABLE TestAggregate;"))
{
statement.Step();
}
}
}
示例9: TestReadOnlyDB
public void TestReadOnlyDB()
{
Exception exception = null;
var numRecords = this.rnd.Next(1, 11);
var insertedRecords = new List<Tuple<int, long, string, double>>(numRecords);
var queriedRecords = new List<Tuple<int, long, string, double>>(numRecords);
for (var i = 0; i < numRecords; i++)
{
insertedRecords.Add(new Tuple<int, long, string, double>(i, this.GetRandomInteger(), this.GetRandomString(), this.GetRandomReal()));
}
using (var connection = new SQLiteConnection(this.databaseRelativePath, SQLiteOpen.READWRITE))
{
using (var statement = connection.Prepare("DROP TABLE IF EXISTS TestReadOnlyDB;"))
{
statement.Step();
}
using (var statement = connection.Prepare("CREATE TABLE TestReadOnlyDB(id INTEGER, i INTEGER, t TEXT, r REAL);"))
{
statement.Step();
}
foreach (var record in insertedRecords)
{
var command = "INSERT INTO TestReadOnlyDB(id, i, t, r) VALUES(" + record.Item1.ToString(this.invClt) + "," + record.Item2.ToString(this.invClt)
+ ",'" + record.Item3 + "'," + record.Item4.ToString(this.invClt) + ");";
using (var statement = connection.Prepare(command))
{
statement.Step();
}
}
}
using (var connection = new SQLiteConnection(this.databaseRelativePath, SQLiteOpen.READONLY))
{
foreach (var record in insertedRecords)
{
var command = "SELECT id, i, t, r FROM TestReadOnlyDB WHERE id = " + record.Item1.ToString(this.invClt) + " AND i = " + record.Item2.ToString(this.invClt)
+ " AND t = '" + record.Item3 + "' AND r = " + record.Item4.ToString(this.invClt) + ";";
using (var statement = connection.Prepare(command))
{
while (statement.Step() == SQLiteResult.ROW)
{
var id = (long)statement[0];
var i = (long)statement[1];
var t = (string)statement[2];
var r = (double)statement[3];
queriedRecords.Add(new Tuple<int, long, string, double>((int)id, i, t, r));
}
}
}
using (var statement = connection.Prepare("DROP TABLE TestReadOnlyDB;"))
{
var result = statement.Step();
if (result == SQLiteResult.READONLY)
{
exception = new SQLiteException(connection.ErrorMessage());
}
else
{
throw new SQLiteException(connection.ErrorMessage());
}
}
}
using (var connection = new SQLiteConnection(this.databaseRelativePath, SQLiteOpen.READWRITE))
{
using (var statement = connection.Prepare("DROP TABLE TestReadOnlyDB;"))
{
var result = statement.Step();
if (result != SQLiteResult.DONE)
{
throw new SQLiteException(connection.ErrorMessage());
}
}
}
Assert.AreEqual(insertedRecords.Count, queriedRecords.Count);
insertedRecords.Sort((x, y) => { return x.Item1 - y.Item1; });
queriedRecords.Sort((x, y) => { return x.Item1 - y.Item1; });
for (var i = 0; i < insertedRecords.Count; i++)
{
var insertedRecord = insertedRecords[i];
var queriedRecord = queriedRecords[i];
Assert.AreEqual(insertedRecord.Item1, queriedRecord.Item1);
Assert.AreEqual(insertedRecord.Item2, queriedRecord.Item2);
Assert.AreEqual(insertedRecord.Item3, queriedRecord.Item3);
Assert.IsTrue(Math.Abs(insertedRecord.Item4 - queriedRecord.Item4) <= Math.Abs(insertedRecord.Item4 * 0.0000001));
//.........这里部分代码省略.........
示例10: TestInMemory
public void TestInMemory()
{
var numRecords = this.rnd.Next(1, 11);
var insertedRecords = new List<Tuple<int, long, string, double>>(numRecords);
var queriedRecords = new List<Tuple<int, long, string, double>>(numRecords);
for (var i = 0; i < numRecords; i++)
{
insertedRecords.Add(new Tuple<int, long, string, double>(i, this.GetRandomInteger(), this.GetRandomString(), this.GetRandomReal()));
}
using (var connection = new SQLiteConnection(":memory:"))
{
using (var statement = connection.Prepare("DROP TABLE IF EXISTS TestInMemory;"))
{
statement.Step();
}
using (var statement = connection.Prepare("CREATE TABLE TestInMemory(id INTEGER, i INTEGER, t TEXT, r REAL);"))
{
statement.Step();
}
foreach (var record in insertedRecords)
{
var command = "INSERT INTO TestInMemory(id, i, t, r) VALUES(" + record.Item1.ToString(this.invClt) + "," + record.Item2.ToString(this.invClt)
+ ",'" + record.Item3 + "'," + record.Item4.ToString(this.invClt) + ");";
using (var statement = connection.Prepare(command))
{
statement.Step();
}
}
foreach (var record in insertedRecords)
{
var command = "SELECT id, i, t, r FROM TestInMemory WHERE id = " + record.Item1.ToString(this.invClt) + " AND i = " + record.Item2.ToString(this.invClt)
+ " AND t = '" + record.Item3 + "' AND r = " + record.Item4.ToString(this.invClt) + ";";
using (var statement = connection.Prepare(command))
{
while (statement.Step() == SQLiteResult.ROW)
{
var id = (long)statement[0];
var i = (long)statement[1];
var t = (string)statement[2];
var r = (double)statement[3];
queriedRecords.Add(new Tuple<int, long, string, double>((int)id, i, t, r));
}
}
}
using (var statement = connection.Prepare("DROP TABLE TestInMemory;"))
{
statement.Step();
}
}
Assert.AreEqual(insertedRecords.Count, queriedRecords.Count);
insertedRecords.Sort((x, y) => { return x.Item1 - y.Item1; });
queriedRecords.Sort((x, y) => { return x.Item1 - y.Item1; });
for (var i = 0; i < insertedRecords.Count; i++)
{
var insertedRecord = insertedRecords[i];
var queriedRecord = queriedRecords[i];
Assert.AreEqual(insertedRecord.Item1, queriedRecord.Item1);
Assert.AreEqual(insertedRecord.Item2, queriedRecord.Item2);
Assert.AreEqual(insertedRecord.Item3, queriedRecord.Item3);
Assert.IsTrue(Math.Abs(insertedRecord.Item4 - queriedRecord.Item4) <= Math.Abs(insertedRecord.Item4 * 0.0000001));
}
}
示例11: TestTransactionRollbackSupport
public void TestTransactionRollbackSupport()
{
var numRecords = this.rnd.Next(1, 11);
var insertedRecords = new List<Tuple<int, long, string, double>>(numRecords);
var queriedRecords = 0;
for (var i = 0; i < numRecords; i++)
{
insertedRecords.Add(new Tuple<int, long, string, double>(i, this.GetRandomInteger(), this.GetRandomString(), this.GetRandomReal()));
}
using (var connection = new SQLiteConnection(this.databaseRelativePath))
{
using (var statement = connection.Prepare("DROP TABLE IF EXISTS TestQuery;"))
{
statement.Step();
}
using (var statement = connection.Prepare("CREATE TABLE TestQuery(id INTEGER, i INTEGER, t TEXT, r REAL);"))
{
statement.Step();
}
var beginTransactionCommand = "BEGIN TRANSACTION";
using (var statement = connection.Prepare(beginTransactionCommand))
{
statement.Step();
}
foreach (var record in insertedRecords)
{
var command = "INSERT INTO TestQuery(id, i, t, r) VALUES(" + record.Item1.ToString(this.invClt) + "," + record.Item2.ToString(this.invClt)
+ ",'" + record.Item3 + "'," + record.Item4.ToString(this.invClt) + ");";
using (var statement = connection.Prepare(command))
{
statement.Step();
}
}
var rollbackTransactionCommand = "ROLLBACK TRANSACTION";
using (var statement = connection.Prepare(rollbackTransactionCommand))
{
statement.Step();
}
foreach (var record in insertedRecords)
{
var command = "SELECT id, i, t, r FROM TestQuery WHERE id = " + record.Item1.ToString(this.invClt) + " AND i = " + record.Item2.ToString(this.invClt)
+ " AND t = '" + record.Item3 + "' AND r = " + record.Item4.ToString(this.invClt) + ";";
using (var statement = connection.Prepare(command))
{
while (statement.Step() == SQLiteResult.ROW)
{
queriedRecords++;
}
}
}
using (var statement = connection.Prepare("DROP TABLE TestQuery;"))
{
statement.Step();
}
}
Assert.AreEqual(0, queriedRecords);
}
示例12: TestChangesCount
public void TestChangesCount()
{
var initialChangesCount = 0;
var insertChangesCount = 0;
var numRecords = this.rnd.Next(1, 11);
var insertedRecords = new List<Tuple<int, long, string, double>>(numRecords);
for (var i = 0; i < numRecords; i++)
{
insertedRecords.Add(new Tuple<int, long, string, double>(i, this.GetRandomInteger(), this.GetRandomString(), this.GetRandomReal()));
}
using (var connection = new SQLiteConnection(this.databaseRelativePath))
{
using (var statement = connection.Prepare("DROP TABLE IF EXISTS TestQuery;"))
{
statement.Step();
}
using (var statement = connection.Prepare("CREATE TABLE TestQuery(id INTEGER, i INTEGER, t TEXT, r REAL);"))
{
statement.Step();
}
initialChangesCount = connection.ChangesCount();
foreach (var record in insertedRecords)
{
var command = "INSERT INTO TestQuery(id, i, t, r) VALUES(" + record.Item1.ToString(this.invClt) + "," + record.Item2.ToString(this.invClt)
+ ",'" + record.Item3 + "'," + record.Item4.ToString(this.invClt) + ");";
using (var statement = connection.Prepare(command))
{
statement.Step();
}
insertChangesCount += connection.ChangesCount();
}
using (var statement = connection.Prepare("DROP TABLE TestQuery;"))
{
statement.Step();
}
}
Assert.AreEqual(0, initialChangesCount);
Assert.AreEqual(insertedRecords.Count, insertChangesCount);
}
示例13: TestEmptyBlob
public void TestEmptyBlob()
{
using (var connection = new SQLiteConnection(this.databaseRelativePath))
{
using (var statement = connection.Prepare("DROP TABLE IF EXISTS TestEmptyBlob;"))
{
statement.Step();
}
using (var statement = connection.Prepare("CREATE TABLE TestEmptyBlob(b BLOB);"))
{
statement.Step();
}
using (var statement = connection.Prepare("INSERT INTO TestEmptyBlob(b) VALUES(@b);"))
{
statement.Bind("@b", this.GetRandomBlob(0));
statement.Step();
statement.Reset();
statement.ClearBindings();
}
using (var statement = connection.Prepare("SELECT b FROM TestEmptyBlob;"))
{
statement.Step();
var emptyBlobValue = statement.GetBlob(0);
Assert.IsNotNull(emptyBlobValue);
Assert.AreEqual(0, emptyBlobValue.Length);
}
using (var statement = connection.Prepare("DROP TABLE TestEmptyBlob;"))
{
statement.Step();
}
}
}
示例14: TestConstantInsertParameterBoundQueryFilter
public void TestConstantInsertParameterBoundQueryFilter()
{
var numRecords = this.rnd.Next(1, 11);
var insertedRecords = new List<Tuple<int, long, string, double>>(numRecords);
var queriedRecords = new List<Tuple<int, long, string, double>>(numRecords);
for (var i = 0; i < numRecords; i++)
{
insertedRecords.Add(new Tuple<int, long, string, double>(i, this.GetRandomInteger(), this.GetRandomString(), this.GetRandomReal()));
}
using (var connection = new SQLiteConnection(this.databaseRelativePath))
{
using (var statement = connection.Prepare("DROP TABLE IF EXISTS TestConstantInsertParameterBoundQueryFilter;"))
{
statement.Step();
}
using (var statement = connection.Prepare("CREATE TABLE TestConstantInsertParameterBoundQueryFilter(id INTEGER, i INTEGER, t TEXT, r REAL);"))
{
statement.Step();
}
foreach (var record in insertedRecords)
{
var command = "INSERT INTO TestConstantInsertParameterBoundQueryFilter(id, i, t, r) VALUES(" + record.Item1.ToString(this.invClt) + "," + record.Item2.ToString(this.invClt)
+ ",'" + record.Item3 + "'," + record.Item4.ToString(this.invClt) + ");";
using (var statement = connection.Prepare(command))
{
statement.Step();
}
}
using (var statement = connection.Prepare("SELECT id, i, t, r FROM TestConstantInsertParameterBoundQueryFilter WHERE id = @id AND i = @i AND t = @t;"))
{
foreach (var record in insertedRecords)
{
statement.Bind(1, record.Item1);
statement.Bind("@i", record.Item2);
statement.Bind(3, record.Item3);
while (statement.Step() == SQLiteResult.ROW)
{
var id = (long)statement[0];
var i = (long)statement[1];
var t = (string)statement[2];
var r = (double)statement[3];
queriedRecords.Add(new Tuple<int, long, string, double>((int)id, i, t, r));
}
statement.Reset();
statement.ClearBindings();
}
}
using (var statement = connection.Prepare("DROP TABLE TestConstantInsertParameterBoundQueryFilter;"))
{
statement.Step();
}
}
Assert.AreEqual(insertedRecords.Count, queriedRecords.Count);
insertedRecords.Sort((x, y) => { return x.Item1 - y.Item1; });
queriedRecords.Sort((x, y) => { return x.Item1 - y.Item1; });
for (var i = 0; i < insertedRecords.Count; i++)
{
var insertedRecord = insertedRecords[i];
var queriedRecord = queriedRecords[i];
Assert.AreEqual(insertedRecord.Item1, queriedRecord.Item1);
Assert.AreEqual(insertedRecord.Item2, queriedRecord.Item2);
Assert.AreEqual(insertedRecord.Item3, queriedRecord.Item3);
Assert.IsTrue(Math.Abs(insertedRecord.Item4 - queriedRecord.Item4) <= Math.Abs(insertedRecord.Item4 * 0.0000001));
}
}
示例15: TestBindParameterFilter
public void TestBindParameterFilter()
{
var numRecords = this.rnd.Next(1, 11);
var insertedRecords = new List<Tuple<int, long, string, double, byte[]>>(numRecords);
var queriedRecords = new List<Tuple<int, long, string, double, byte[]>>(numRecords);
for (var i = 0; i < numRecords; i++)
{
insertedRecords.Add(new Tuple<int, long, string, double, byte[]>(i, this.GetRandomInteger(), this.GetRandomString(), this.GetRandomReal(), this.GetRandomBlob()));
}
using (var connection = new SQLiteConnection(this.databaseRelativePath))
{
using (var statement = connection.Prepare("DROP TABLE IF EXISTS TestBindParameterFilter;"))
{
statement.Step();
}
using (var statement = connection.Prepare("CREATE TABLE TestBindParameterFilter(id INTEGER, i INTEGER, t TEXT, r REAL, b BLOB);"))
{
statement.Step();
}
using (var statement = connection.Prepare("INSERT INTO TestBindParameterFilter(id, i, t, r, b) VALUES(@id,@i,@t,@r,@b);"))
{
foreach (var record in insertedRecords)
{
statement.Bind(1, record.Item1);
statement.Bind("@i", record.Item2);
statement.Bind(3, record.Item3);
statement.Bind("@r", record.Item4);
statement.Bind(5, record.Item5);
statement.Step();
statement.Reset();
statement.ClearBindings();
}
}
using (var statement = connection.Prepare("SELECT id, i, t, r, b FROM TestBindParameterFilter WHERE id = @id AND i = @i AND t = @t AND r = @r AND b = @b;"))
{
foreach (var record in insertedRecords)
{
statement.Bind(1, record.Item1);
statement.Bind("@i", record.Item2);
statement.Bind(3, record.Item3);
statement.Bind("@r", record.Item4);
statement.Bind(5, record.Item5);
while (statement.Step() == SQLiteResult.ROW)
{
var id = (long)statement[0];
var i = (long)statement[1];
var t = (string)statement[2];
var r = (double)statement[3];
var b = (byte[])statement[4];
queriedRecords.Add(new Tuple<int, long, string, double, byte[]>((int)id, i, t, r, b));
}
statement.Reset();
statement.ClearBindings();
}
}
using (var statement = connection.Prepare("DROP TABLE TestBindParameterFilter;"))
{
statement.Step();
}
}
Assert.AreEqual(insertedRecords.Count, queriedRecords.Count);
insertedRecords.Sort((x, y) => { return x.Item1 - y.Item1; });
queriedRecords.Sort((x, y) => { return x.Item1 - y.Item1; });
for (var i = 0; i < insertedRecords.Count; i++)
{
var insertedRecord = insertedRecords[i];
var queriedRecord = queriedRecords[i];
Assert.AreEqual(insertedRecord.Item1, queriedRecord.Item1);
Assert.AreEqual(insertedRecord.Item2, queriedRecord.Item2);
Assert.AreEqual(insertedRecord.Item3, queriedRecord.Item3);
Assert.IsTrue(Math.Abs(insertedRecord.Item4 - queriedRecord.Item4) <= Math.Abs(insertedRecord.Item4 * 0.0000001));
Assert.IsTrue(insertedRecord.Item5.SequenceEqual(queriedRecord.Item5));
}
}