[Mobile GIS] Using SQLite (2)

Source: Internet
Author: User

It is best to store tiles using SQLite. Is it slow to copy so many images when creating offline maps? Tired?

Mbtiles uses SQLite and has a rule to store tiles. What is mbtiles? Refer to my blog;
[Mobile GIS] mbtiles mobile storage is not described here.

Mbtiles is actually an SQLite database, but this database creates tables according to some rules to store tile information, so it is called mbtiles, the suffix of the data file generated by SQLite can be changed without affecting your use. SQLite or. dB, and then store the tiles. When we generate a database file, we generate it. mbtiles format

 

In the Android mobile storage process, you do not need to add SQLite JDBC. It seems that the android SDK supports SQLite. How can we import tiles into the SQLite database? No way, you can only write a program, traverse the local tiles, and insert them to SQLite in sequence. below is the key code for inserting the tiles:

Package COM. EHL. SQLite; import Java. io. bytearrayoutputstream; import Java. io. file; import Java. io. fileinputstream; import Java. SQL. connection; import Java. SQL. drivermanager; import Java. SQL. preparedstatement; import Java. SQL. sqlexception; import Java. SQL. statement;/*** SQLite data operation ** @ author faz.pdf. zheng **/public class sqlitedb {Private Static statement; Private Static connection = N Ull; Private Static preparedstatement insertps = NULL; Private Static preparedstatement deleteps = NULL; Public sqlitedb (string mbtilespath) throws classnotfoundexception, sqlexception {class. forname ("org. SQLite. JDBC "); If (mbtilespath = NULL | mbtilespath. trim (). length () = 0) {connection = drivermanager. getconnection ("JDBC: SQLite: ehl_mbtiles_db.mbtiles"); Statement = connection. createstatement (); inittable () ;} Else {connection = drivermanager. getconnection ("JDBC: SQLite:" + mbtilespath); Statement = connection. createstatement ();} insertps = connection. preparestatement ("insert into tiles (zoom_level, tile_row, tile_column, tile_data) values (?,?,?,?) "); Deleteps = connection. preparestatement (" delete from tiles where zoom_level =? And tile_row =? And tile_column =? ");}/*** Initialize table information * @ throws sqlexception */private void inittable () throws sqlexception1_statement.exe cute ("Drop table if exists metadata" without using statement.exe cute ("Drop table if exists tiles" without using statement.exe cute ("create table metadata (Name text, value text) "initialize statement.exe cute (" create table tiles (zoom_level integer, tile_row integer, tile_column integer, tile_data BLOB) "initialize statement.exe cute (" create unique Index tile_index on tiles (zoom_level, tile_row, tile_column) "using statement.exe cute (" create unique index name on metadata (name )"); statement.exe cute ("insert into metadata values ('name', 'ehl _ mbtiles_db ')" inserting into statement.exe cute ("insert into metadata values ('type', 'baselayer ') "initialize statement.exe cute (" insert into metadata values ('version', '1. 0 ') "initialize statement.exe cute (" insert into metadata values ('desscr Iption ', 'mobile GIS') "using repeated statement.exe cute (" insert into metadata values ('format', 'png ')");} /*** insert a data record * @ Param zoom_level * @ Param tile_row * @ Param tile_column * @ Param tile_data */Public void inserttiles (string zoom_level, string tile_row, string tile_column, file tile_data) {fileinputstream FCM; bytearrayoutputstream Bos = NULL; try {connection. setautocommit (false); FCM = new fileinputstream (tile_da Ta); Bos = new bytearrayoutputstream (); byte [] Buf = new byte [1024]; int readnum; while (readnum = FS. Read (BUF ))! =-1) {Bos. write (BUF, 0, readnum);} insertps. setstring (1, zoom_level); insertps. setstring (2, tile_row); insertps. setstring (3, tile_column); insertps. setbytes (4, Bos. tobytearray (); deleteps. setstring (1, zoom_level); deleteps. setstring (2, tile_row); deleteps. setstring (3, tile_column#;eps.executeupdate(;;insertps.exe cuteupdate (); connection. commit ();} catch (exception e) {try {connection. rollback ();} Ca Tch (sqlexception E1) {e1.printstacktrace ();} e. printstacktrace () ;}}/*** close some objects */Public void close () {try {If (insertps! = NULL) insertps. Close (); If (deleteps! = NULL) deleteps. Close (); If (statement! = NULL) statement. Close (); If (connection! = NULL) connection. Close () ;}catch (sqlexception e) {system. Err. println (e );}}}

 

Related Article

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.