SQLite with swift , i can't get all elements from my table query - xcode

I'm new in swift and SQLLite , I had a problem and I didn't know how to fix it
I create table contain [id name Grade] and this is my insert function
let insertStatementString = "INSERT INTO Contact (Id, Name, Grade ) VALUES (?, ?, ?);"
func insert() {
var insertStatement: COpaquePointer = nil
// 1
if sqlite3_prepare_v2(db, insertStatementString, -1, &insertStatement, nil) == SQLITE_OK {
let dic: [NSString] = ["Ray", "Chris", "Martha", "Danielle"]
let grade = [11 , 13 ,11 ,12]
var id = Int32()
for (index, name) in dic.enumerate() {
sqlite3_bind_int(insertStatement, 1, index + 1)
sqlite3_bind_text(insertStatement, 2, name.UTF8String, -1, nil)
sqlite3_bind_int(insertStatement, 3,Int32(grade[index]))
id = id + 1
}
if sqlite3_step(insertStatement) == SQLITE_DONE {
print("Successfully inserted row.")
} else {
print("Could not insert row.")
}
} else {
print("INSERT statement could not be prepared.")
}
// 5
sqlite3_finalize(insertStatement)
}
insert()
when i call my query function , the while loop work for one time only , but as you can see i had 4 elements in my table
this is my query function
let queryStatementString = "SELECT * FROM Contact;"
func query() {
var queryStatement: COpaquePointer = nil
if sqlite3_prepare_v2(db, queryStatementString, -1, &queryStatement, nil) == SQLITE_OK {
while (sqlite3_step(queryStatement) == SQLITE_ROW) {
let id = sqlite3_column_int(queryStatement, 0)
let queryResultCol1 = sqlite3_column_text(queryStatement, 1)
let grade = sqlite3_column_int(queryStatement, 2)
let name = String.fromCString(UnsafePointer<CChar>(queryResultCol1))!
print("Query Result:")
print("\(id) | \(name) | \(grade)")
}
} else {
print("SELECT statement could not be prepared")
}
sqlite3_finalize(queryStatement)
}
query()
last thing , this my create Table function
let db = openDatabase()
let createTableString = "CREATE TABLE Contact(" + "Id INT PRIMARY KEY NOT NULL," + "Name CHAR(255)," + "Grade INTEGER)"
func createTable() {
var createTableStatement: COpaquePointer = nil
if sqlite3_prepare_v2(db, createTableString, -1, &createTableStatement, nil) == SQLITE_OK {
if sqlite3_step(createTableStatement) == SQLITE_DONE {
print("Contact table created.")
} else {
print("Contact table could not be created.")
}
} else {
print("CREATE TABLE statement could not be prepared.")
}
sqlite3_finalize(createTableStatement)
}
createTable()

In your insert() function, you call sqlite3_step() once. This is why you insert a single row. Move the call to sqlite3_step() inside your loop on names and grades. Also call sqlite3_reset() before setting bindings.

Related

How work history of web browser under the hood

I am trying to implement my own web browser history for WKWebView on iOS, but I can't implement this functionality completely, and each time I obtain trouble.
I can create a history where the user did be and then moving forward and backward inside history.
But I have next trouble, and I think it an only one of many problems on my way.
When I have a history with for example 10 elements, and then I am moving back to element number 5 and then go don't forward but try to open the new link I can't remove element 6-10 and put the new link.
I think my problem that I can't fully understand how history work inside all browsers under the hood, this is not a hard task but I am confused inside this algorithm.
My main data structure for holding history
Help me understand how to work this algorithm inside browsers or maybe exist a good theory about it?
I have solved this problem and realize the full algorithm well, the completed project available here: https://github.com/IhorYachmenov/Custom-browser-history-for-WKWebView.
Algorithm:
struct PlayerHistory {
static var shared = PlayerHistory()
var historyExist: Bool = false
var historyCurrentPosition: Int = 0
var historyLastPositionBeforeUpdatingHistory: Int!
var userHistoryKey: String!
var backPressed: Bool!
var forwardPressed: Bool!
var urlOfPlayer: String!
// Function only for first loading inside <viewDidLoad or another method from app LifeCycle>.
mutating func getUrlForFirstLoading(initURL: String, key: String) -> String {
urlOfPlayer = initURL
guard HistoryStorage.shared.getHistoryFromUserDefaults() != nil else {
updateFirstElement(key: key, url: initURL)
return initURL
}
guard HistoryStorage.shared.getHistoryFromUserDefaults()![key] != nil else {
return initURL
}
let position = HistoryStorage.shared.getHistoryFromUserDefaults()![key]!.count - 1
historyExist = true
historyCurrentPosition = position
userHistoryKey = key
let initUrlFromHistoryStorage = HistoryStorage.shared.getHistoryFromUserDefaults()![key]!.last!.url
return initUrlFromHistoryStorage
}
// Create new or update exist history, use this method indsede <decidePolicyForNavigation>.
mutating func updatePlayerHistory(backlisk: [String], key: String) {
var history = [WebViewHistory]()
for i in backlisk {
history.append(WebViewHistory(i))
}
if (historyExist == true) {
// If old history exist need compound both and then to save.
let oldHistory = HistoryStorage.shared.getHistoryFromUserDefaults()![key]
let oldAndNewHostoryTogether = oldHistory! + history
var keyValuePair = Dictionary<String, [WebViewHistory]>()
keyValuePair.updateValue(oldAndNewHostoryTogether, forKey: key)
HistoryStorage.shared.removeHistory()
HistoryStorage.shared.saveHistory(keyValuePair)
setCurrentPosition(url: backlisk.last!, key: key)
} else {
var keyValuePair = Dictionary<String, [WebViewHistory]>()
keyValuePair.updateValue(history, forKey: key)
historyExist = true
HistoryStorage.shared.removeHistory()
HistoryStorage.shared.saveHistory(keyValuePair)
setCurrentPosition(url: backlisk.last!, key: key)
}
}
// Before using this method check if result don't equals nil. Use this method for navigation beetween history
func moveThroughHistory(key: String, direction: Bool) -> String? {
guard historyExist != false else {
return nil
}
let history = HistoryStorage.shared.getHistoryFromUserDefaults()![key]!
if (direction == true) {
let index = historyCurrentPosition + 1
guard index != history.count else { return nil }
return history[index].url
} else {
let index = historyCurrentPosition - 1
guard index > 0 else { return history[0].url }
return history[index].url
}
}
// Method <setCurrentPosition> each time set position at history
mutating func setCurrentPosition(url: String, key: String) {
guard HistoryStorage.shared.getHistoryFromUserDefaults() != nil else { return }
guard HistoryStorage.shared.getHistoryFromUserDefaults()![key] != nil else { return }
let history = HistoryStorage.shared.getHistoryFromUserDefaults()![key]
let index = history?.firstIndex(of: WebViewHistory(url))
guard index != nil else {
historyCurrentPosition = 0
return
}
historyCurrentPosition = index!
}
// <removeUnusedPeaceOfHistory> need use when user want open new page staying inside the middle of history
mutating func removeUnusedPeaceOfHistory(key: String) {
guard HistoryStorage.shared.getHistoryFromUserDefaults() != nil else {
return
}
guard HistoryStorage.shared.getHistoryFromUserDefaults()![key] != nil else {
return
}
var history = HistoryStorage.shared.getHistoryFromUserDefaults()![key]!
let startIndex = historyCurrentPosition + 1
let endIndex = history.endIndex - 1
let countOfAllElements = history.count
guard startIndex != countOfAllElements else { return }
let range = startIndex...endIndex
history.removeSubrange(range)
var keyValuePair = Dictionary<String, [WebViewHistory]>()
keyValuePair.updateValue(history, forKey: key)
HistoryStorage.shared.removeHistory()
HistoryStorage.shared.saveHistory(keyValuePair)
}
// Use <updateFirstElement> inside <getUrlForFirstLoading> if history doesn't exist
private mutating func updateFirstElement(key: String, url: String) {
var history = [WebViewHistory]()
history.insert(WebViewHistory(url), at: 0)
var keyValuePair = Dictionary<String, [WebViewHistory]>()
keyValuePair.updateValue(history, forKey: key)
HistoryStorage.shared.saveHistory(keyValuePair)
historyExist = true
historyCurrentPosition = 0
}
// Use <webViewWillBeClosedSaveHistory> when WKWebView should be closed, if the user moves through history new position will be saved.
mutating func webViewWillBeClosedSaveHistory(key: String) {
let history = HistoryStorage.shared.getHistoryFromUserDefaults()![key]!
let currentPosition = historyCurrentPosition + 1
guard currentPosition != history.count else { return }
removeUnusedPeaceOfHistory(key: key)
}
}

How to yield a nilable shared object in Chapel?

Currently, I am working on Chapel Data Objects and I am facing some issues. I have a class named Row which can be nil.
I have these three methods:
override iter these()ref {
for row in this.fetchall(){
yield row;
}
}
override proc fetchone(): shared Row? {
if this.curRow == this.numRows {
return nil;
}
var row = new shared Row();
var rowNum: int(32) = 0;
mysql_data_seek(this.res, this.curRow: c_int);
var _row = mysql_fetch_row(this.res);
while(rowNum < this.nFields) {
var datum: string = __get_mysql_row_by_number(_row, rowNum: c_int);
var colinfo = this.getColumnInfo(rowNum);
row.addData(colinfo.name,datum);
rowNum += 1;
}
this.curRow += 1;
return row;
}
override iter fetchall(): borrowed Row {
var res: shared Row? = this.fetchone();
while(res != nil) {
yield res!;
res = this.fetchone();
}
}
As you can see that in one case I am returning a borrowed object while in other cases I am returning shared. Is there any way such that I only return one type, either shared or borrowed as currently in situations like this I am getting error.
var res: Row? = cursor.fetchone();
for row in cursor {
res = row;
//get row data by column name and print it.
writeln("name = ", row["name"]," email = ", row["email"] );
}
The idea is to typecast.
override iter fetchall(): shared Row {
var res: shared Row? = this.fetchone();
while(res != nil) {
try! {
yield res: shared Row;
}
res = this.fetchone();
}
}

Dynamically Set Column Name In LINQ Query

Im trying to write a method which will allow me to search different DataTables, over different columns.
So far i have the following:
string selectedValue;
string searchColumn;
string targetColumn;
var results = (from a in dt.AsEnumerable()
where a.Field<string>(searchColumn) == selectedValue
select new
{
targetColumn = a.Field<string>(targetColumn)
}).Distinct();
Which kind of gets the job done, but I'm left with the column name as targetColumn rather than the actual column name I want.
Is there any way to resolve this?
Thanks in advance
CM
I make a LINQ to Datatables
public List<DataRow> Where(this DataTable dt, Func<DataRow, bool> pred)
{
List<DataRow> res = new List<DataRow>();
try {
if (dt != null && dt.Rows.Count > 0) {
for (i = 0; i <= dt.Rows.Count - 1; i++) {
if (pred(dt(i))) {
res.Add(dt(i));
}
}
}
} catch (Exception ex) {
PromptMsg(ex);
}
return res;
}
Usage :
var RowsList = dt.Where(f => f("SomeField").toString() == "SomeValue" ||
f("OtherField") > 5);

Optimize queries for Union, Except, Join with LINQ and C#

I have 2 objects (lists loaded from XML) report and database (showed bellow in code) and i should analyse them and mark items with 0, 1, 2, 3 according to some conditions
TransactionResultCode = 0; // SUCCESS (all fields are equivalents: [Id, AccountNumber, Date, Amount])
TransactionResultCode = 1; // Exists in report but Not in database
TransactionResultCode = 2; // Exists in database but Not in report
TransactionResultCode = 3; // Field [Id] are equals but other fields [AccountNumber, Date, Amount] are different.
I'll be happy if somebody could found time to suggest how to optimize some queries.
Bellow is the code:
THANK YOU!!!
//TransactionResultCode = 0 - SUCCESS
//JOIN on all fields
var result0 = from d in database
from r in report
where (d.TransactionId == r.MovementID) &&
(d.TransactionAccountNumber == long.Parse(r.AccountNumber)) &&
(d.TransactionDate == r.MovementDate) &&
(d.TransactionAmount == r.Amount)
orderby d.TransactionId
select new TransactionList()
{
TransactionId = d.TransactionId,
TransactionAccountNumber = d.TransactionAccountNumber,
TransactionDate = d.TransactionDate,
TransactionAmount = d.TransactionAmount,
TransactionResultCode = 0
};
//*******************************************
//JOIN on [Id] field
var joinedList = from d in database
from r in report
where d.TransactionId == r.MovementID
select new TransactionList()
{
TransactionId = d.TransactionId,
TransactionAccountNumber = d.TransactionAccountNumber,
TransactionDate = d.TransactionDate,
TransactionAmount = d.TransactionAmount
};
//Difference report - database
var onlyReportID = report.Select(r => r.MovementID).Except(joinedList.Select(d => d.TransactionId));
//TransactionResultCode = 1 - Not Found in database
var result1 = from o in onlyReportID
from r in report
where (o == r.MovementID)
orderby r.MovementID
select new TransactionList()
{
TransactionId = r.MovementID,
TransactionAccountNumber = long.Parse(r.AccountNumber),
TransactionDate = r.MovementDate,
TransactionAmount = r.Amount,
TransactionResultCode = 1
};
//*******************************************
//Difference database - report
var onlyDatabaseID = database.Select(d => d.TransactionId).Except(joinedList.Select(d => d.TransactionId));
//TransactionResultCode = 2 - Not Found in report
var result2 = from o in onlyDatabaseID
from d in database
where (o == d.TransactionId)
orderby d.TransactionId
select new TransactionList()
{
TransactionId = d.TransactionId,
TransactionAccountNumber = d.TransactionAccountNumber,
TransactionDate = d.TransactionDate,
TransactionAmount = d.TransactionAmount,
TransactionResultCode = 2
};
//*******************************************
var qwe = joinedList.Select(j => j.TransactionId).Except(result0.Select(r => r.TransactionId));
//TransactionResultCode = 3 - Transaction Results are different (Amount, AccountNumber, Date, )
var result3 = from j in joinedList
from q in qwe
where j.TransactionId == q
select new TransactionList()
{
TransactionId = j.TransactionId,
TransactionAccountNumber = j.TransactionAccountNumber,
TransactionDate = j.TransactionDate,
TransactionAmount = j.TransactionAmount,
TransactionResultCode = 3
};
you may try something like below:
public void Test()
{
var report = new[] {new Item(1, "foo", "boo"), new Item(2, "foo2", "boo2"), new Item(3, "foo3", "boo3")};
var dataBase = new[] {new Item(1, "foo", "boo"), new Item(2, "foo22", "boo2"), new Item(4, "txt", "rt")};
Func<Item, bool> inBothLists = (i) => report.Contains(i) && dataBase.Contains(i);
Func<IEnumerable<Item>, Item, bool> containsWithID = (e, i) => e.Select(_ => _.ID).Contains(i.ID);
Func<Item, int> getCode = i =>
{
if (inBothLists(i))
{
return 0;
}
if(containsWithID(report, i) && containsWithID(dataBase, i))
{
return 3;
}
if (report.Contains(i))
{
return 2;
}
else return 1;
};
var result = (from item in dataBase.Union(report) select new {Code = getCode(item), Item = item}).Distinct();
}
public class Item
{
// You need also to override Equals() and GetHashCode().. I omitted them to save space
public Item(int id, string text1, string text2)
{
ID = id;
Text1 = text1;
Text2 = text2;
}
public int ID { get; set; }
public string Text1 { get; set; }
public string Text2 { get; set; }
}
Note that you need to either implement Equals() for you items, or implement an IEqualityComparer<> and feed it to Contains() methods.

AdvancedDataGrid Column Sorting Question

I have am using an mx:advanceddatagrid and I running into a problem with grouped column sorting. A screenshot of what my grid looks like is here: http://i.stack.imgur.com/waAi7.png
I have the following kind of data:
PRODUCT_ID PROD_NAME CATEGORY TYPE SubType SubType_descr
1 p_1 c1_y t1
t1_s1 sub_1*
t1_s2 sub_2*
t1_s4 sub_4*
t1_s5 sub_5*
2 p_2 c1_x t1
t1_s1 sub_1
t1_s2 sub_2
t1_s3 sub_3
3 p_3 c1_x t1
t1_s1 sub_1
t1_s2 sub_2*
t1_s3 sub_3*
Now, the dataset is of the type arrayCollection. I manually move it into GroupingCollection2 when a setter fires the Binding Event;
protected function setGrouping(columnNames:Array):void{
_groupingColumns = [];
if (columnNames.length > 0) {
var _groupedData:GroupingCollection2 = new GroupingCollection2();
var _grouping:Grouping = new Grouping();
for (var i:int=0;i<columnNames.length;i++) {
var gc:GroupingField = new GroupingField(columnNames[i]);
_groupingColumns.push(gc);
}
_groupedData.source = _ds;
_grouping.fields = _groupingColumns;
_groupedData.grouping = _grouping
_groupedData.refresh();
_data = _groupedData;
} else {
_data = _ds;
}
setGridColumns();
}
Although I am grouping on Product_Id, i achieve the above look by a labelFunction as shown below:
col.labelFunction = function (item:Object,column:AdvancedDataGridColumn):String {
if (item.hasOwnProperty('GroupLabel') && item.GroupLabel != null) {
if (item.children.length > 0) {
return item.children[0][column.dataField];
}
else {
return "";
}
}
else {
return "";
}
}
The data I recieve from the server is of the kind shown below
PRODUCT_ID PROD_NAME CATEGORY TYPE SubType SubType_descr
-------------------------------------------------------------------
1 p_1 c1_y t1 t1_s1 sub_1*
2 p_2 c1_y t1 t1_s2 sub_2*
3 p_3 c1_y t1 t1_s3 sub_3*
-------------------------------------------------------------------
4 p_4 c1_x t1 t1_s1 sub_1*
5 p_5 c1_x t1 t1_s2 sub_2*
6 p_6 c1_x t1 t1_s3 sub_3*
In short I am taking all the common column values (Product, Category, Type) and putting them in the first row, all the changing values in the sub-rows (sub_type, subtype_descr)
I would like to sort this gird on Prod_Name, Category and type columns as well but sonething like the following is not working, even though the underlying data gets sorted.
public var _sort_direction:Boolean = false;
protected function adg1_headerReleaseHandler(event:AdvancedDataGridEvent):void
{
var sort:Sort = new Sort();
sort.fields = [new SortField(event.dataField, true, _sort_direction)];
_ds.sort = sort;
_ds.refresh();
if (DefaultGroupColumn != null && temp[0] != DefaultGroupColumn) {
temp.push(DefaultGroupColumn);
}
setGrouping(temp);
adg1.validateNow();
_sort_direction = !_sort_direction;
}
all help is really appreciated.
The answer to this was so simple, but Adobe documentation and some other blogs sent me on a wild goose chase...
for (var i:int=0;i<columnNames.length;i++) {
var gc:GroupingField = new GroupingField(columnNames[i]);
gc.compareFunction = positionCompareFunction;
gc.descending = sort_dir;
_groupingColumns.push(gc);
}
A colleague (thanks Samitt) helped me figure this out. Just add gc.descending and the grouping will sort on the grouped column.

Resources