C#/Java连接sqlite与使用技巧

无意中发现的,C#/Java连接sqlite与使用技巧。看了下,还挺不错的。与大家分享一下。

1)下载sqlite jdbc驱动http://www.xerial.org/maven/repository/artifact/org/xerial/sqlite-jdbc/

2)将下载的驱动加入eclipse项目的built path中

3)示例代码:

复制代码 代码如下:

package com.hedalixin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class test {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db");
        Statement stat = conn.createStatement();
        stat.executeUpdate("drop table if exists people;");
        stat.executeUpdate("create table people (name, occupation);");
        PreparedStatement prep = conn
                .prepareStatement("insert into people values (?, ?);");

        prep.setString(1, "Gandhi");
        prep.setString(2, "politics");
        prep.addBatch();
        prep.setString(1, "Turing");
        prep.setString(2, "computers");
        prep.addBatch();
        prep.setString(1, "Wittgenstein");
        prep.setString(2, "smartypants");
        prep.addBatch();
        conn.setAutoCommit(false);
        prep.executeBatch();
        conn.setAutoCommit(true);
        ResultSet rs = stat.executeQuery("select * from people;");
        while (rs.next()) {
            System.out.println("name = " + rs.getString("name"));
            System.out.println("job = " + rs.getString("occupation"));
        }
        rs.close();
        conn.close();

    }
}

2.  C#连接sqlite
2.1 使用SQLITE.NET
SQLite.NET也是一个数据访问组件,其中的System.Data.SQLite 就好像是.NET自带的System.Data.SqlClient一样。里面包含了connection、command等数据访问的常用对象,只是他们前面都有一个前缀sqlite。
1)下载System.Data.SQLite,下载地址http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

2)  通过Add References引用SQLite ADO .NET安装目录的bin目录下的System.Data.SQLite.DLL。

3)创建表、读取数据等和Access或MS SQL没多大区别

复制代码 代码如下:

//创建一个数据库文件
string datasource="h:/test.db";
System.Data.SQLite.SQLiteConnection.CreateFile(datasource);
//连接数据库
System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection();
System.Data.SQLite.SQLiteConnectionStringBuilder connstr = new System.Data.SQLite.SQLiteConnectionStringBuilder();
connstr.DataSource = datasource;
connstr.Password = "admin";//设置密码,SQLite ADO.NET实现了数据库密码保护
conn.ConnectionString = connstr.ToString();            
conn.Open();
//创建表
System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand();
string sql = "CREATE TABLE test(username varchar(20),password varchar(20))";
cmd.CommandText=sql;
cmd.Connection=conn;
cmd.ExecuteNonQuery();
//插入数据
sql = "INSERT INTO test VALUES('ekinglong','mypassword')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
//取出数据
sql = "SELECT * FROM test";
cmd.CommandText = sql;
System.Data.SQLite.SQLiteDataReader reader = cmd.ExecuteReader();
StringBuilder sb = new StringBuilder();
while (reader.Read())
{
     sb.Append("username:").Append(reader.GetString(0)).Append("\n")
     .Append("password:").Append(reader.GetString(1));

 }
 MessageBox.Show(sb.ToString());

以上就是C#/Java连接sqlite与使用技巧的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程