One up to develop the weather software for Android (ii)

Source: Internet
Author: User
Tags sqlite database

Thank you for your support and attention to this series of blog posts, we are now hot to formally start the development of our Android weather software! Have not read before about the function of the software requirements of the students can first look together to develop the weather software for Android (a), you can go to a quick glance, clear our profile system. What we are going to do today is to build the SQLite database, and what information does this database store? The database is used to store a number of provinces and cities nationwide information, a total of 34 provinces such as Zhejiang Province and there are many cities such as Hangzhou, Wenzhou, Huzhou, Hangzhou and a lot of counties, we have to do is to put these structured data stored in our database.

So let's get started now! This time we use the Litepal Open source framework, it uses the Object Relational mapping (ORM) pattern, and we usually develop the most commonly used in the database functions are encapsulated, so that you do not have to write a line of SQL statements can be completed a variety of building tables, add and revise the operation of the check. In short, it is very convenient to use, from the Guo Lin great God's Blog High School will understand the framework, I quickly learn to use this weather software programming, Pratiece makes PREFERCT, we will see under the power of Litepal!

First of all, we need to set up a province,city,county three sheet.

1, Province


2. City


3, County


OK, above this is the structure of our table, we will find that each table will have an ID, and then have the corresponding Province_code,province_name property, in addition, City has a province_id column, which is a foreign key column, In order to achieve a relationship between the provincial table and the city table, a city table contains a specific province ID, and allows multiple cities to store the same province ID, so that a town can only correspond to one province, but a province may have more than one city, but also to achieve a multi-to-one relationship! And so on, County also has a city_id to achieve the city and the county's one-to-many relationship, do not know that you see here are clear that there is no our three-sheet relationship!


we know the above table structure, we can formally enter the table operation, this way I am re-outlining the use of the Litepal process, if you have not downloaded this jar package of classmates, Litepal Open Source project address:https://github.com/LitePalFramework/LitePal download, first in your own program import jar package, in the Project Assets directory under the new Litepal.xml file, the third step in Androidmanifest.xml configuration litepalapplication, three steps to complete the package of the import process!

After using Litepal, we don't have to "create table province (" + "ID integer primary key autoincrement," + "province_name varchar," + "provi Nce_code varchar "); Such a statement of the table, everything became very simple. According to the concept of object-relational mapping mode, each table should correspond to a model, that is, if we need to build a provinces table first, there should be a corresponding province model class. What tables are needed to create a corresponding class, what column is in the model class corresponding to what kind of field, let us look! Provinces Class!

Package Com.melhc.model;import Java.util.arraylist;import Java.util.list;import org.litepal.crud.DataSupport; public class Province extends Datasupport {private int id;private string Province_name;private string Province_code;priva Te list<city> cities = new arraylist<city> ();p ublic String Getprovince_code () {return province_code;} public void Setprovince_code (String province_code) {this.province_code = Province_code;} Public list<city> GetCities () {return cities;} public void Setcities (list<city> cities) {this.cities = cities;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String Getprovince_name () {return province_name;} public void Setprovince_name (String province_name) {this.province_name = Province_name;}}

The above is our provinces class, we can see that the model class has id,province_name,provice_code and cities fields, and all implement the Get Set method, this way must pay attention to implement Getset method, Or the building will fail! In addition, the id attribute can be written without writing, Litepal and humanized automatic generation, focus on our side there is a collection of city, this is what to do, is to describe a province corresponding to the city, so set up a list collection, Then how does the city table show the relationship of many to one?

package Com.melhc.model ; Import Java.util.arraylist;import Java.util.list;import org.litepal.crud.datasupport;public class City extends datasupport {private int id;private string City_name;private string City_code;private province province;private List< county> counties = new arraylist<county> ();p ublic list<county> getcounties () {return counties;} public void Setcounties (list<county> counties) {this.counties = counties;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String Getcity_name () {return city_name;} public void Setcity_name (String city_name) {this.city_name = City_name;} Public String Getcity_code () {return city_code;} public void Setcity_code (String city_code) {this.city_code = City_code;} Public province Getprovince () {return province;} public void Setprovince (province province) {this.province = province;}}  

From the above, we just set up a province class in the city table, that each city corresponds to a province attribution, good said here everyone should understand how to achieve it! careful friends will also find that each of our classes also inherit a datesupport, this class is to achieve Litepal database additions and deletions to change the operation of the ha!

Ibid. county table is as follows!

Package Com.melhc.model;import Org.litepal.crud.datasupport;public Class County extends Datasupport {private int id; private string County_name;private string county_code;private city city;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String Getcounty_name () {return county_name;} public void Setcounty_name (String county_name) {this.county_name = County_name;} Public String Getcounty_code () {return county_code;} public void Setcounty_code (String county_code) {this.county_code = County_code;} Public City Getcity () {return city;} public void Setcity (city city) {this.city = city;}}
OK, through the above operation we have completed the establishment of three tables, do not forget one thing in assets file Litepal.xml file modified

<?xml version= "1.0" encoding= "Utf-8"?><litepal>    <dbname value= "Weather" >    </dbname >    <version value= "2" >    </version>    <list>        <mapping class= " Com.melhc.model.Province ">        </mapping>        <mapping class=" com.melhc.model.City ">        </ mapping>        <mapping class= "Com.melhc.model.County" >        </mapping>    </list></ Litepal>

OK, the following in order to facilitate our database operation, we encapsulate a WEATHERDB class, the implementation of the database of the formal generation and additions and deletions to change the operation!

/** * Store Provice instance to database */public void Saveprovice (province province) {if (province! = null) {Province.save ();}}

We can see that when storing province information, the method simply passes in a province class, Then call the Province.save method can implement the database insert, then how to insert the database these fields, we just need to create a new province class, and then the network read the information through Province.setprovice_name () When the set method is passed in, is it very simple? Province province = new province ();p Rovince.setprovince_code (array[0]); Province.setprovince_name (array[1]);

So here's the question. How do we implement the relationship between the table and the table of city and province, in fact, it is very simple, as long as the city corresponding to the province get, and the set method of construction into the good! Let's take a look.

                                         City City = new City (); City.setcity_code (array[0]); City.setcity_name (array[1]); City.setprovince (province); Weatherdb.savecity (city);
isn't it very simple! Then how to read it, this is more simple, set up a province class list collection, call Datesupport FindAll () method can be obtained, query the way there are many, I do not list on this side, you can also add a lot of queries to delete the conditions and so on.
/** * Read all provinces of the country from the database */public list<province> loadprovices () {list<province> List = Datasupport.findall ( Province.class); return list;}
so how to query the city table data? We know that each city corresponds to a province province, then we first through the associated with the province_id to find its corresponding province class, the problem is resolved more than half, You can then return to the list of all cities below the province by using the GetCities () method of this province class!

/** * Read all the saved city information from the database */public list<city> loadCities (int provinceid) {Province provice = Datasupport.find ( Province.class, provinceid,true); list<city> list = Provice.getcities (); return list;}
The good key points are all done, let's take a look at the whole picture of this class!

Package Com.melhc.db;import Java.util.list;import Org.litepal.crud.datasupport;import Org.litepal.tablemanager.connector;import Com.melhc.model.city;import Com.melhc.model.county;import Com.melhc.model.province;import Android.database.sqlite.sqlitedatabase;public class WeatherDB {/** * Some basic database method encapsulation */ Private sqlitedatabase db;private static Weatherdb weatherdb;public Weatherdb () {//TODO auto-generated constructor STUBD b = connector.getdatabase ();//formally Generate database}public synchronized static Weatherdb getinstance () {if (Weatherdb = = null) {Weather DB = new Weatherdb ();} return WEATHERDB;} /** * Store Provice instance to database */public void Saveprovice (province province) {if (province! = null) {Province.save ();}} /** * Read all provinces of the country from the database */public list<province> loadprovices () {list<province> List = Datasupport.findall ( Province.class); return list;} /** * Store City instance to database */public void Savecity (city city) {if (city! = null) {City.save ();}} /** * Read all the saved city information from the database */public list<city> loadCities (int provinceId) {Province provice = Datasupport.find (Province.class, provinceid,true); list<city> list = Provice.getcities (); return list;} /** * Store County instance to database */public void Savecounty (county county) {if (county! = null) {County.save ();}} /** * Read all county information from a database */public list<county> loadcounties (int cityid) {City town = datasupport.find (City.class, CI Tyid,true); list<county> list = City.getcounties (); return list;} /** * Close database */public void Destroydb () {if (db! = null) {Db.close ();}}}

OK, the content of this lesson is here, the first blog post code so many words, feel Meng Meng da Ah, I hope that we can have a better understanding of the Litepal framework through this blog post, but also hope that we can continue to support the series of blog, your support is my greatest power to write down! today's database design is over, the next blog post will soon meet you.

Here is the Git open source address for the app, Https://github.com/melhc/SimpleWeather






One up to develop the weather software for Android (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.