Course Content
Local Database
Ø ApplicationProgramProcessing Data
Baby name eliminator is an application for retrieving baby names by inputting personality traits (my wife and I use this method to name two sons ). Unlike the method of name extraction by brainstorming, and then worry about missing the best name, this application allows us to name a baby using the elimination method.
The baby name eliminator is built on a huge database that stores 36,065 boys and 60,438 girls in the United States. After we select a gender, the application uses multiple filters to narrow down the name list. These filters are based on the following factors: the popularity of each name, the name's start and end letters, and the first time the name was used. After filtering the list, we can exclude the names one by one until we make the final selection.
When naming a child, we will consider multiple times to exclude the obvious bad ones, leaving us hesitant. When we calm down and treat 20 selectable names, my wife and I each choose the 5 best. For the first son, we chose the same name, so we made the final decision. If both you and your wife have a Windows Phone, you can select a name until you list the candidates. This method is very interesting.
So, where does the database come from? The answer is the social security agency, which provides names on almost every social security card since 1880. There are several notes for this table:
For privacy reasons, the period only contains names that are used more than 5 times in a given year.
The name of a single character is not included.
Many people born before December 1937 do not have social security cards, so the data for those years is incomplete.
Different spelling rules with the same name are considered as different names.
The shard data has not been processed or troubleshooting. Sometimes the gender on the application form is incorrect, resulting in the name list of girls containing boys. In addition, some names are recorded as "unknown", "unnamed", or "baby ". You can solve these problems by limiting the first 1000 names in the list.
To enable this filtering, this application uses two local databases-one for boys and the other for girls.
Working with local databases
Windows Phone 7's lack of support for local databases is one of its acknowledged shortcomings. We encourage the application to work with the database on the server, but this introduces additional burden for developers and brings more troubles (latency, data connection, potential data traffic cost ). Fortunately, we can select a third-party database. My favorite is the open-source SQLite for Windows Phone 7, which was created by Dan Ciprian ardelean. We can view the details on the following webpage. Use the following link to obtain the latest version: http://www.neologics.eu/dan/wp7_sqlite_2020.9.2010.zip. It contains C #Source codeAnd a community. csharpsqlite. WP. dll file. We can reference it in the project. It certainly cannot have no defects, but in most cases it works very well (for example, this application ).
SQLite for Windows Phone 7 reads or writes database files from the isolated bucket. If we want to deploy the database with data filled with applications, we can include the database files in the project and set build action to content. Before using SQLite for the first time at runtime, our application obtains the file and stores it in an isolated bucket. You can call application. getresourcestream to access the file in the form of content in the project.
How to Create a. BD file containing databases deployed with the application?
The following are the methods in the Windows Phone application:
1. Run the create table and insert commands to generate a database using SQLite.
2. Obtain the raw data of the. DB file stored in the isolated bucket by SQLite using the API.
3. Copy the character data from Visual Studio debugger as a base64 encoded string and use another (desktop) program to decode them and store them in the required. DB file.
Note:
When a worker executes a large number of database operations, in order to make the user interface responsive, the interaction with SQLite is completed through the backgroundworker background thread, and the success/failure information is obtained through the callback function.
The command string passed by explain to executescalar and executequery can be an SQL command, for example, select count (*) from table
Executequery is a common method. Its common parameter T must be a class and has an attribute consistent with the column selected in the query.
Application. getresourcestream works with files contained in the project. The build action of the file is set to content, or the build action is set to resource. For the latter, the imported URI must comply with the following syntax:/dllname; component/pathandfilename
Note that dllname can reference any DLL in The. xap file as long as it contains the required resources. However, it cannot contain the DLL suffix.
For this application, the databasename string of the boy name database (boys. DB) located in the project root directory is as follows,
/Windowsphoneapp; component/boys. DB
However, in this case, the SaveFile used in list 24.1 must be changed, because the databasename string is no longer a valid file name for the isolated bucket.
Application. getresourcestream and assembly. getmanifestresourcestream
We may accidentally find that the Assembly. getmanifestresourcestream API is used to read files contained in the application. This is feasible, but it is limited to the files that build actions are set to embedded Resource (rather than set to resource ).
However, the string passed to getmanifestresourcestream has its unique syntax: dllname in dllname. filename is the name of the DLL containing the embedded resource. That is because when each embedded resource is named, the C # compiler automatically deducts the DLL name (minus. dll extension) in front of the file name (you can use such. net reflector tool to open the DLL to view the name ). In this application, two valid strings are "windowsphoneapp. Boys. DB" and "windowsphoneapp. Girls. DB ".
Compared with this method, application. getresourcestream is more flexible. Compared with other mechanisms that use files as embedded resources, the getresourcestream method that uses files as content is more popular because resources increase the size of DLL files, in this way, the loading time of the application is increased.
The filter page
We can browse the included sourcesCodeTo view the application's home page, but first we need to view the filter page using the databasehelper class. The filter page 24.1 shows the number of names in the list. Then, we can use some options to filter them. These options can be mapped to SQL query commands, it also works on the database (the names of boys and girls are selected on the previous homepage ).
Fig 24.1
As shown in figure 24.2, each button will reveal a new window or page, allowing you to control each related filter condition. Click the number of names to display the list of real names, as shown in Figure 24.3. This list does not support interactive exclusion, but it can be completed on the home page.
Fig 24.2
Fig 24.3
Note:
The dialog box that appears when you click the dibutton is created by the dialog user control, which can be seen in the source code of the application. The subtitle grid is created by the lettergrid user control. The grid here is similar to the quickjumpgrid control in Chapter 18th "Cocktails.
The five filter buttons in the dig command are of the toggle type, and their ischecked statuses are managed by the code behind them. If a filter condition is activated, its related buttons are checked (highlighted) so that you can see it without clicking each button, you do not need to perform a double check on the filter conditions.
When the supervisor executes a query in the background thread, the progress bar and its related user interface are displayed, as shown in Figure 24.4. Because it does not occupy the entire screen, if the user does not care about waiting for the current name, it allows the user to continue working.
This project contains two databases (boys. DB and girls. DB), which have the same pattern. They only contain a table named names, which has three columns: name, bestrank (the best ranking in a single year) and firstyear (the first year in the Social Security database ).
The query command for refreshing the number of names by distinct is "select count (*) from names". It can have a where clause, which is based on the settings determined by the filtering rule.
The query command for displaying the real name of explain is "Select name from names", which can also have the WHERE clause. Therefore, the record class used with executequery has the name attribute of a single string. The tostring method allows the record set to be used as a data source for list box preview without any data template, because the built-in tostring in the text block is sufficient.
just like Date Picker in the previous chapter, this application uses bidirectional data binding for each character.