Something about sqlite3_finalize

I face a very strange problem today. I use some code to connect to the database I create.I create a very simple code to replace a row of data in my database.But it doesn’t work.It is strange that it can connect to the database, it can get a row of data, but it can not insert a row of data. It took me two hours to read my code, try to find out what’s wrong with my code.Finally, I found that it is because of a statement  which I did not finalize .

Although I use the code to connect and operate the database, I do not have a further understanding about the code I use, which results the strange problem I faced today.

Thus, I search on the Internet and found something about the sqlite3_finalize

Destroy A Prepared Statement Object

int sqlite3_finalize(sqlite3_stmt *pStmt);

The sqlite3_finalize() function is called to delete a prepared statement. If the most recent evaluation of the statement encountered no errors or if the statement is never been evaluated, then sqlite3_finalize() returns SQLITE_OK. If the most recent evaluation of statement S failed, then sqlite3_finalize(S) returns the appropriateerror code or extended error code.

The sqlite3_finalize(S) routine can be called at any point during the life cycle of prepared statement S: before statement S is ever evaluated, after one or more calls to sqlite3_reset(), or after any call to sqlite3_step() regardless of whether or not the statement has completed execution.

Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.

The application must finalize every prepared statement in order to avoid resource leaks. It is a grievous error for the application to try to use a prepared statement after it has been finalized. Any use of a prepared statement after it has been finalized can result in undefined and undesirable behavior such as segfaults and heap corruption.

 

From the above text which is from development document, we know that we need to finalize every statement we use.

In my original code, I just return the sqlite3_column_int(statement,0), which means that I do not finalize the statement.

Thus, I change it to:

The problem is solved.

 

The document about the operation of the database is in : Press here.

A confusing problem about storyboard and its solution

Problem:

To demonstrate the whole problem, I need to show the design view at first.

I need to press the Set As Default button to change the view.Beside, I need to do some jobs when press the Set As Default button.

Thus, I connected the IBAction function to the action of Set As Default buttons.And I connect the Set As Default button to the first view at the storyboard.

However, although the value of the first view should have been changed by the code in the Set As Default function, it is not correct.(The value is still not changed.)

This problem confused me for a long time.

Solution:

I try to use NSLog to track the running process of the software.

In my original opinion, I consider that the machine will first response to the action of the Set As Default button. Then, the machine will  release all the resource of second view.After that, the machine will load the first view finally.

Actually, the behavior of the machine is different from I expect. According to the NSLog, the machine loads the first view to response to the storyboard, then responses to the action of the Set As Default button.(That’s why the code in the action function which is connected to the Set As Default button has no effect on the first view.)

How to use the pickerview ?

Goal: Use the pickerview to select a country.

Steps:

1.

Add a pickerview on the view.

Attention: If you did not configure the pickerview, it will not display when running.

2.

Connect the pickerview on the view with the property on the .h file.

3.

The interface of the controller must obey the UIPikcerViewDelegate and UIPickerViewDataSource

4.

Because of the protocol, we need to rewrite some functions.

Here we need to understand some concepts about the pickerview.

1.What’s pickerData?

PickerData is the data source of the pickerview.

2.What’s component?

A pickerview may have some components.(Here we only have one component.)

4.

We need to add some codes on the viewDidView

pickerData = [[NSMutableArray alloc] init];

[pickerView selectRow:0 inComponent:0 animated:YES];

[self pickerView:pickerView didSelectRow:0 inComponent:0];

When view is loaded, we need to allocate a NSMutableArray to initilize the pickerData

Then we need to select the first row. We must also use the didSelectRow to motivate the selected action function to fulfilled the purpose (Maybe change the value of a textfield.)

Final result is :

Attention:

The code above is only the most important part of the pickerview used to finish some functions. It does not mean you can create a pickerview according to these exact steps.

Objective-C: Protocols and Delegation

Check your understanding

What’s a protocol?

How to define a protocol?

What’s “NSCopying” about?

How to adopt multiple protocols?

What happens to the subclass if the base class adopt protocols?

What happens if you adopt a protocol without implementing its optional methods?

How to check to see whether an object conform to a protocol, an optional method of a protocol?

What’s a delegation?

…. to be continued

How to get the address of the location?

Goal: Get the location(string) by the CLLocation

Procedure:

According to some material on the Internet, I try to use MKreversegeocoder through MKreversegeocoderDelegate protocol  at first.

However, when I use this protocol, I found that the all the method of MKreversegeocoder is out-dated.

Thus, I search the apple development document again and find  I can use CLGeocoder to replace MKreversegeocoder.

 

Steps:

1.

Abstract a function to add annotation with the given title and subtitle.

2.

 

In the scope {} is the code of handler, which means that if the CLGeocoder finished the query, it will pass the controller to the code in the scope to handle it.

 

The result:

 

 

 

How to add the current location on the mapview

Goal:Add the current location on the  mapview with the annotation “My location”

Steps:

1.

Add a mapview on the view

2.

add an interface of annotation.

The interface inherits NSObject and obey the MKAnnotation protocol

The interface includes an  CLLocationCorordinate2D variable which is used to locate the current location

The title is what you want to display on the red pin.

The content of .m file is very simple. You only need to rewrite the dealloc function of the interface.

 

3.

Add IBOutlet on the .h file of view controller.And relate it with the mapview.(Press cirl key to do it.It is a basic operation.)

4.

What we want is press the locate botton to locate my current location on the map. Thus, we need to write some code in the action function of the locate botton.

 

The main idea of this code is:

1.Allocate the location manager and use some operation to obtain the current location

2.use mapview setRegion to set a red pin at the location

3.allocate a displaymap interface(The displaymap interface are written by ourselves in the second step).Set annotation title.

ATTENTION: If we do not detect if ann is nil, the program will face a problems.If the user of the program press locate button many time.The code will repeat new a displaymap interface.As a result, the shadow of the red pin will be darker and darker, which we do not want.

 

 

How to add custom keyboard at IOS Development

Goal:

Add a custom keyboard at IOS development

Reason:

Original keyboard could not fulfill our requirements. For example, if we need a keyboard  which can surport positive numbers.

Main idea:

Use the view I add to cover the inputview of the text field.

Steps:

1.add .h and .m files to the project

2.

add a view to the project.(This view is use for custom keyboard)

3.

Design the keyboard.

4.

Here,

There are two views in the project.

1. The view which the text view is in.

2. custom keyboard.

I need to write the view controller for the second view. And we need to write a protocol for the view controller of the first view to use.

.h

The view controller of the first view surport the implement of the OwnNumberKeyBoardDelegate protocol and pass itself to the delegate variable in the second view’s controller.Thus, when the view controller of the second view capture some action, it can control the first view’s controller to do the suitable action. For example, when the second view controller capture a backspace action, the action function in the second view controller use delegate value to make the text in textfield to backspace.

5.

We add some code to the viewDidLoad function of the first view’s controller.

Here we cover the input view of the tipsration(which is a textfield) by the custom keyboard’s controller.

Of course,the view controller of the first view needs to implement the OwnNumberKeyBoardDelegate which is written in the .h file of the second view.(We also need to import the second view controller’s .h file)

We need to write the code for the actions of backspace or number input .These are not in the range of demonstrating in this post.

6.

The result will be: