SQLite operations

first to create a database
need to write a class as follow

public class SQLiteDB extends SQLiteOpenHelper {

    public static final String CREATE_DB =  "create table data ( "+
            "id integer primary key autoincrement, "+
            "name text)";

    private Context mContext;

    public SQLiteDB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(CREATE_DB);
        Toast.makeText(mContext, "the table has been created", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

then we can use it

private SQLiteDB sqLiteDB ;
sqLiteDB = new SQLiteDB(mContext, "sqldata.db", null, 1);

then this step would create a new database, just once. would not create later.

sqLiteDB.getWritableDatabase();

2/ write to database

SQLiteDatabase db = sqLiteDB.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("name", "firstone");
                db.insert("data" , null, values);
                values.clear();

3/ read database

SQLiteDatabase db = sqLiteDB.getReadableDatabase();
//        Cursor cursor = db.query("data",);
        Cursor cursor1 = db.rawQuery("select * from data", new String[]{});

        List<String> names = new ArrayList<String>();
         while(cursor1.moveToNext())
        {
            names.add(  cursor1.getString(1));
        }

In summary:
database opereations–
1/ create,

    db.execSQL(CREATE_DB); on class database override oncreate;

2/ write,

        ContentValues values = new ContentValues();
        values.put("key", "value");-->db.insert("table",null, values);

3/ query

        Cursor cursor = db.rawQuery("SQL", new String[]{});
        cursor.getString(columns);/getBoolean or sth.

4/ delete

        db.delete("Book", "pages > ?", new String[] { "500" });

5/ update

        values.put("price", 10.99);
        db.update("Book", values, "name = ?", new String[] { "The Da
        Vinci Code" });
JSP(Java Server Pages)是一种用于开发动态网页的技术,它基于 Java 编程语言并通过在 HTML 页面中插入 Java 代码片段来实现动态内容生成。在 JSP 中,可以使用 Java 程序访问数据库并从中检索、插入或更新数据。而 SQLite 是一种轻量级且独立于服务器的嵌入式数据库引擎,它被广泛应用于移动设备和嵌入式系统等领域。 为了在 JSP 中使用 SQLite 数据库,我们需要将 SQLite 的 JAR 文件添加到项目的类路径中。SQLite 提供了一个名为 sqlite-jdbc 的 Java 驱动程序,我们可以从官方网站或 Maven 仓库下载并导入项目。在 JSP 页面中,可以使用 Java 代码来加载 SQLite 驱动程序并建立与数据库的连接。连接成功后,我们可以通过执行 SQL 查询语句来与数据库进行交互,例如检索数据或插入新的记录。 在 JSP 页面中,可以使用以下代码加载 SQLite 驱动程序并与数据库建立连接: ``` <%@ page import="java.sql.*" %> <% Connection connection = null; try { Class.forName("org.sqlite.JDBC"); connection = DriverManager.getConnection("jdbc:sqlite:/path/to/database.db"); // database operations here } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } %> ``` 以上代码首先导入了 java.sql 包,然后加载了 SQLite 驱动程序并使用 DriverManager 建立了与数据库的连接。连接 URL 中可以指定 SQLite 数据库文件的路径。在连接建立后,可以执行适当的 SQL 查询语句,如 SELECT 或 INSERT 语句,通过 ResultSet 获取查询结果并在 JSP 页面中进行显示。 总结来说,JSP 是用于开发动态网页的技术,可以通过使用 SQLite 的 Java 驱动程序来访问 SQLite 数据库。通过加载驱动程序并建立与数据库的连接,可以执行 SQL 查询语句以实现数据库的检索和更新操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值