使用SQLite帮助您获取listview数据库 (获取listview数据库)

SQLite是一种轻量级的关系型数据库管理系统,它适用于小型的应用程序和嵌入式设备。虽然SQLite没有提供诸如MySQL或Oracle等更大型的数据库管理系统中的高级功能,但是它可以轻松地嵌入到应用程序中,并且查询速度非常快。在Android应用程序开发中,SQLite作为默认的嵌入式数据库经常被使用。下面,我们将介绍如何。

之一步:建立SQLite数据库

需要在应用程序中建立SQLite数据库。您可以使用Android开发工具(ADT)的SQLite Database Browser来创建数据库。在ADB shell的命令行界面中,输入以下命令来创建数据库:

sqlite3

代表您想要创建的数据库的名称。执行以上命令后,ADT将在你的应用程序中自动生成一个SQLite数据库。您可以通过运行以下命令在ADB shell 中查看您的数据库:

.sqlite_master中

这个命令将列出您在数据库中创建的所有表。

第二步:创建表

接下来,您需要为您的应用程序创建表。SQLite使用SQL查询语言来建立表格。例如,以下是用于建立一个名为“student”的表的查询语句:

create table student ( id integer primary key autoincrement, name text not null, age integer not null );

在此查询语句中,您可以看到表格包含3个字段:id(整数类型,主键和自动增量),name(文本类型,不能为空),age(整数类型,不能为空)。当您运行这个查询语句时,SQLite会在您的数据库中创建一个新表“students”。

第三步:插入数据

在您的表中建立后,就可以开始向其中添加数据。使用INSERT查询语句可以将数据写入表中。例如,以下是向“student”表中添加一行数据的查询语句:

insert into student (name, age) values (‘Tom’, 18);

在此查询语句中,您可以看到我们将“Tom”和“18”作为数据对填入到“student”表格的“name”和“age”字段中。同样,您可以使用相同的INSERT查询语句向表格添加更多的数据。

第四步:查询数据

一旦您已将数据插入到表中,就可以开始从表格中检索数据。使用SELECT查询语句可以从表中检索数据。例如,以下是查询“student”表中所有数据的查询语句:

select * from student;

在此查询语句中,我们使用“*”通配符来检索表格中所有数据。如果您只需要检索特定行或列的数据,则可以使用更复杂的查询语句。

第五步:在ListView中显示数据

现在您已经成功地使用SQLite在您的应用程序中创建了数据库,并向其中添加数据。下一步是以一种美观友好的方式在您的应用程序中显示这些数据。您可以使用Android的ListView控件来显示SQLite数据库中的数据。使用CursorAdapter可以使ListView控件与SQLite数据库进行交互,并显示数据库中的数据。例如,以下是ListView控件显示从“student”表中检索的数据的代码:

Cursor cursor = db.query(“student”, null, null, null, null, null, null);

String[] from = { “name”, “age” }; int[] to = { R.id.name, R.id.age };

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listview_item, cursor, from, to, 0);

listView.setAdapter(adapter);

在此代码中,我们首先使用Cursor对象从“student”表中检索所有数据。然后,我们使用from和to数组来指定应在ListView控件中显示哪些字段。我们使用SimpleCursorAdapter将Cursor对象与ListView控件进行连接。

结论

相关问题拓展阅读:

如何从sqlite数据库中获取数据并显示在listview中

在登录页面后,我想在listview中把Apple显示成A,Boy显示成B等尘轮罩等,直到F。但派闹是在程序中当我完全登录后,只有登录表成功创建,主菜单还是没有创建。

我想桐友在test database中创建主菜单,然后我想从主菜单表(mainmenu table)中获取数据再显示在listview中。

我使用了下面的代码:

if(username.length()>0&&password.length()>0)

{

SQLiteAdapter db=new SQLiteAdapter(Main.this);

db.openToWrite();

if(db.Login(username,password))

{

System.out.println(“goutham”);

Intent intent=new Intent(getApplicationContext(),ExampleActivity.class);

startActivity(intent);

}

SQLiteAdapter.java

}

public Cursor queueAll() {

String columns = new String { KEY_ID, KEY_CONTENT };

Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null,

null, null, null, null);

return cursor;

}

private static class SQLiteHelper extends SQLiteOpenHelper {

public SQLiteHelper(Context context, String name,

CursorFactory factory, int version) {

super(context, name, factory, version);

}

@Override

public void onCreate(SQLiteDatabase db) {

// TODO Auto-generated method stub

db.execSQL(SCRIPT_CREATE_DATABASE);

db.execSQL(DATABASE_CREATE);

}

@Override

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

// TODO Auto-generated method stub

}

}

public long AddUser(String username, String password) {

ContentValues initialValues = new ContentValues();

initialValues.put(KEY_USERNAME, username);

initialValues.put(KEY_PASSWORD, password);

return sqLiteDatabase.insert(DATABASE_TABLE, null, initialValues);

}

public boolean Login(String username, String password) {

// TODO Auto-generated method stub

Cursor mCursor = sqLiteDatabase.rawQuery(“SELECT * FROM “

+ DATABASE_TABLE + ” WHERE username=? AND password=?”,

new String { username, password });

if (mCursor != null) {

if (mCursor.getCount() > 0) {

return true;

}

}

return false;

}

}

ExampleActivity.java

public class ExampleActivity extends Activity {

private SQLiteAdapter mySQLiteAdapter;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ListView listContent = (ListView) findViewById(R.id.contentlist);

/*

* Create/Open a SQLite database and fill with dummy content and close

* it

*/

mySQLiteAdapter = new SQLiteAdapter(this);

mySQLiteAdapter.openToWrite();

// mySQLiteAdapter.deleteAll();

mySQLiteAdapter.insert(“A for Apply”);

mySQLiteAdapter.insert(“B for Boy”);

mySQLiteAdapter.insert(“C for Cat”);

mySQLiteAdapter.insert(“D for Dog”);

mySQLiteAdapter.insert(“E for Egg”);

mySQLiteAdapter.insert(“F for Fish”);

mySQLiteAdapter.close();

/*

* Open the same SQLite database and read all it’s content.

*/

mySQLiteAdapter = new SQLiteAdapter(this);

mySQLiteAdapter.openToRead();

Cursor cursor = mySQLiteAdapter.queueAll();

startManagingCursor(cursor);

String from = new String { SQLiteAdapter.KEY_CONTENT };

int to = new int { R.id.text };

SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,

R.layout.row, cursor, from, to);

listContent.setAdapter(cursorAdapter);

mySQLiteAdapter.close();

}

}

运行程序后,登录表在数据库中创建了,但是主菜单表没有创建。运行程序后,显示一个错误:

sqlite returned code=1 no such a table in MY_TABLE

通过互联网整理获得以下解决方法:

=================1楼=====================

Database class:

public String getData1() throws SQLException{

// TODO Auto-generated method stub

String columns1 = new String { KEY_DATE };

Cursor c1 = ourDatabase.query(DATABASE_MARKSTABLE, columns1, null, null, null,

null, KEY_ENDINGTIME+” DESC”, ” 30″);

String result1 = “”;

int isName = c1.getColumnIndex(KEY_DATE);

for (c1.moveToFirst(); !c1.isAfterLast(); c1.moveToNext()) {

result1 = result1 + c1.getString(isName)

+ ” ” + “\n”;

}

c1.close();

return result1;

}

代码:前蠢

if(username.length()>0&&password.length()>改昌0)

{

SQLiteAdapter db=new SQLiteAdapter(Main.this);

db.openToWrite();

if(db.Login(username,password))

{

System.out.println(“核悔扒goutham”);

Intent intent=new Intent(getApplicationContext(),ExampleActivity.class);

startActivity(intent);

}

关于获取listview数据库的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。


数据运维技术 » 使用SQLite帮助您获取listview数据库 (获取listview数据库)