Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Query the required data through SQL statements

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

这篇文章主要讲解了"通过SQL语句查询需要的数据",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"通过SQL语句查询需要的数据"吧!

在main.xml中:

在MyDatabaseHelper.java类中:

package com.li.sqlite;

//数据库的辅助操作类

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

public class MyDatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASENAME = "liyewen.db" ;

private static final int DATABASERVERSION = 1 ; // 设置数据库的版本

private static final String TABLENAME = "mytab" ;

public MyDatabaseHelper(Context context) { // 用户最关心的也肯定只是Context

super(context, DATABASENAME, null, DATABASERVERSION);

}

@Override

public void onCreate(SQLiteDatabase db) { // 创建数据表

String sql = "CREATE TABLE " + TABLENAME + "("

+ "id INTEGER PRIMARY KEY ," // 在SQLite中设置为Integer、PRIMARY KEY则ID自动增长

+ "name VARCHAR(50) NOT NULL ,"

+ "birthday DATE NOT NULL" + ")";

db.execSQL(sql) ; // 执行SQL

System.out.println("****************** 创建:onCreate()。");

}

@Override

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

String sql = "DROP TABLE IF EXISTS " + TABLENAME ;

db.execSQL(sql) ;

System.out.println("****************** 更新:onUpgrade()。");

this.onCreate(db) ;

}

}

在MytabCursor.java类中:

package com.li.sqlite;

import java.util.ArrayList;

import java.util.List;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

public class MytabCursor {

private static final String TABLENAME = "mytab" ;

private SQLiteDatabase db = null ;

public MytabCursor(SQLiteDatabase db) {

this.db = db ;

}

public List find(){

List all = new ArrayList() ; // 此时只是String

String sql = "SELECT id,name,birthday FROM " + TABLENAME ;

Cursor result = this.db.rawQuery(sql, null); // 执行查询语句

for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext()) { // 采用循环的方式检索数据

all.add("【" + result.getInt(0) + "】" + " " + result.getString(1)

+ "," + result.getString(2));

}

this.db.close() ;

return all ;

}

}

在MySQLiteDemo.java中:

package com.li.sqlite;

import android.app.Activity;

import android.database.sqlite.SQLiteOpenHelper;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.ListView;

public class MySQLiteDemo extends Activity {

private Button findBut = null;

private SQLiteOpenHelper helper = null;

private LinearLayout mylayout = null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.main);

this.findBut = (Button)super.findViewById(R.id.findBut);

this.mylayout = (LinearLayout)super.findViewById(R.id.mylayout);

this.findBut.setOnClickListener(new OnClickListenerImpl());

}

private class OnClickListenerImpl implements OnClickListener{

public void onClick(View v) {

MySQLiteDemo.this.helper = new MyDatabaseHelper(MySQLiteDemo.this);

ListView listView = new ListView(MySQLiteDemo.this);

listView.setAdapter( //设置数据

new ArrayAdapter //所有的数据是字符串

(MySQLiteDemo.this, //上下文

android.R.layout.simple_list_item_1, //列表显示的布局

new MytabCursor( //实例化查询

MySQLiteDemo.this.helper.getReadableDatabase()) //取得SQLiteDatabase对象

.find())); //调用find()方法,返回List

MySQLiteDemo.this.mylayout.addView(listView);

}

}

}

感谢各位的阅读,以上就是"通过SQL语句查询需要的数据"的内容了,经过本文的学习后,相信大家对通过SQL语句查询需要的数据这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report