Java Operation SQLite Database

Source: Internet
Author: User
Tags sqlite sqlite database

Java Operation SQLite Database steps:

1. Import SQLite JDBC

This article uses Sqlite-jdbc-3.7.2.jar, Http://pan.baidu.com/s/1kVHAGdD

2. Writing the JDBC Connection code

 Public classOpsqlitedb {Private Static FinalString class_name = "Org.sqlite.JDBC"; Private Static FinalString Callmarkdb_url = "Jdbc:sqlite:f:\\xxxdatabase.db";  Public Static voidMain (String args[]) {//load the SQLITE-JDBC driver using the current class loaderConnection Connection =NULL; Try{Connection=createconnection ();            Func1 (connection); System.out.println ("Success!"); }  Catch(SQLException e) {System.err.println (E.getmessage ()); } Catch(Exception e) {e.printstacktrace (); } finally{            Try {                if(Connection! =NULL) Connection.close (); } Catch(SQLException e) {//connection close failed.System.err.println (e); }        }    }        //Create a SQLite database connection     Public StaticConnection CreateConnection ()throwsSQLException, ClassNotFoundException {class.forname (class_name); returndrivermanager.getconnection (Callmarkdb_url); }     Public Static voidFunc1 (Connection Connection)throwsSQLException {Statement Statement=connection.createstatement (); Statement statement1=connection.createstatement (); Statement.setquerytimeout (30);//set Timeout to the SEC. //Execute Query StatementResultSet rs = statement.executequery ("SELECT * from Table_name1");  while(Rs.next ()) {String col1= Rs.getstring ("Col1_name"); String col2= Rs.getstring ("Col2_name"); System.out.println ("col1 =" + col1 + "col2 =" +col2);            SYSTEM.OUT.PRINTLN (location); //perform an INSERT statement operationStatement1.executeupdate ("INSERT into table_name2 (col2) VALUES ('" + Col2_value + "')"); //Execute UPDATE statementStatement1.executeupdate ("Update table_name2 set field Name 1 =" + Field value 1 + "where field name 2 = '" + Field value 2 + "'"); }    }

Here are some other SQL statements from the URL:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059182.html (using SQLite on Android)

Query statement:

SELECT * from table name where conditional clause GROUP by group sentence having ... order by sort clause

Such as:

SELECT * FROM person

SELECT * FROM person ORDER BY id DESC

Select name from the person group by name has COUNT (*) >1


Paging SQL is similar to MySQL, the following SQL statement gets 5 records, skipping the previous 3 records

SELECT * From account limit 5 offset 3 or select * From account limit 3,5


Insert statement:

Insert into Table name (field list) VALUES (value list). Example: INSERT into person (name, age) VALUES (' Wisdom ', 3)


UPDATE statement:

Update table name SET field name = value WHERE Condition clause. such as: Update person set name= ' preach wisdom ' where id=10


Delete statement:

Delete from table name where conditional clause. such as: delete from the person where id=10

Java Operation SQLite Database

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.