安卓查询mysql一条数据_android怎么从数据库读取一条数据赋值给字符串

展开全部

您好,请您把SimpleCursorAdapter全部替换掉:

DatabaseActivity.java

package com.poqop.database;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import android.app.Activity;

import android.database.Cursor;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.widget.CursorAdapter;

import android.widget.ListView;

import android.widget.SimpleCursorAdapter;

import android.widget.Toast;

import com.poqop.R;

public class DatabasesActivity extends Activity {

public LayoutInflater m;

ListView listView;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.maintable);

DBAdapter db = new DBAdapter(this);

try {

String destPath = "/data/data/" + getPackageName() +

"/databases";

File f = new File(destPath);

if (true) {//!f.exists()) {

f.mkdirs();

f.createNewFile();

//---copy the db from the assets folder into

// the databases folder---

CopyDB(getBaseContext().getAssets().open("mydb"),

new FileOutputStream(destPath + "/MyDB"));

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

listView=(ListView)findViewById(R.id.show3);

db.open();

for(int a=1;a<=3;a++)

{

int rowID=a;

Cursor c = db.getTitle(rowID);

if (c.moveToFirst())

inflateList(c);

else

Toast.makeText(this, "No title found",

Toast.LENGTH_LONG).show();

}

db.close();

}

public void CopyDB(InputStream inputStream,

OutputStream outputStream) throws IOException {

//---copy 1K bytes at a time---

byte[] buffer = new byte[1024];

int length;

while ((length = inputStream.read(buffer)) > 0) {

outputStream.write(buffer, 0, length);

}

inputStream.close();

outputStream.close();

}

public void inflateList(Cursor c)

{

// 填充SimpleCursorAdapter

SimpleCursorAdapter adapter = new SimpleCursorAdapter(

DatabasesActivity.this,

R.layout.line, c,

new String[] { "_id", "name","email" }

, new int[] {R.id.my_title, R.id.my_content,R.id.my_content2},

CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); //③62616964757a686964616fe59b9ee7ad9431333337383937

// 显示数据

listView.setAdapter(adapter);

}

}

DBAdapter.java

package com.poqop.database;

import java.util.HashMap;

import com.poqop.R;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.SQLException;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.util.Log;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;

public class DBAdapter

{

public static final String KEY_ROWID = "_id";

public static final String KEY_NAME = "name";

public static final String KEY_EMAIL = "email";

private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "MyDB";

private static final String DATABASE_TABLE = "contacts";

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =

"create table titles (_id integer primary key autoincrement, "

+ "isbn text not null, title text not null, "

+ "publisher text not null);";

private final Context context;

private DatabaseHelper DBHelper;

private SQLiteDatabase db;

public DBAdapter(Context ctx)

{

this.context = ctx;

DBHelper = new DatabaseHelper(context);

}

private static class DatabaseHelper extends SQLiteOpenHelper

{

DatabaseHelper(Context context)

{

super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override

public void onCreate(SQLiteDatabase db)

{

db.execSQL(DATABASE_CREATE);

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion,

int newVersion)

{

Log.w(TAG, "Upgrading database from version " + oldVersion

+ " to "

+ newVersion + ", which will destroy all old data");

db.execSQL("DROP TABLE IF EXISTS titles");

onCreate(db);

}

}

//---打开数据库---

public DBAdapter open() throws SQLException

{

db = DBHelper.getWritableDatabase();

return this;

}

//---关闭数据库---

public void close()

{

DBHelper.close();

}

//---向数据库中插入一个标题---

public long insertTitle(String name, String email)

{

ContentValues initialValues = new ContentValues();

initialValues.put(KEY_NAME, name);

initialValues.put(KEY_EMAIL, email);

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

}

//---删除一个指定标题---

public boolean deleteTitle(long rowId)

{

return db.delete(DATABASE_TABLE, KEY_ROWID +

"=" + rowId, null) > 0;

}

//---检索所有标题---

public Cursor getAllTitles()

{

return db.query(DATABASE_TABLE, new String[] {

KEY_ROWID,

KEY_NAME,

KEY_NAME},

null,

null,

null,

null,

null);

}

//---检索一个指定标题---

public Cursor getTitle(long rowId) throws SQLException

{

Cursor mCursor =

db.query(true, DATABASE_TABLE, new String[] {

KEY_ROWID,

KEY_NAME,

KEY_EMAIL},

KEY_ROWID + "=" + rowId,

null,

null,

null,

null,

null);

if (mCursor != null) {

mCursor.moveToFirst();

}

return mCursor;

}

//---更新一个标题---

public boolean updateTitle(long rowId, String name,

String email)

{

ContentValues args = new ContentValues();

args.put(KEY_NAME, name);

args.put(KEY_EMAIL, email);

return db.update(DATABASE_TABLE, args,

KEY_ROWID + "=" + rowId, null) > 0;

}

}

maintable.xml

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#DEDCD2"

android:stretchColumns="*">

android:background="#DEDCD2"

android:layout_margin="0.5dip">

android:text="CA"

android:textColor="#0000FF"

android:textSize="20dip"

android:textStyle="bold"

android:ellipsize="marquee" />

android:text="Device"

android:textColor="#0000FF"

android:textSize="20dip"

android:textStyle="bold"

android:ellipsize="marquee" />

android:text="FixPage"

android:gravity="center"

android:textColor="#0000FF"

android:textSize="20dip"

android:textStyle="bold"

android:ellipsize="marquee" />

android:id="@+id/show3"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:drawSelectorOnTop="false"

/>

line..xml

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal" >

android:id="@+id/my_title"

android:layout_width="47dp"

android:layout_height="wrap_content"

android:layout_weight="0.27" />

android:id="@+id/my_content"

android:layout_width="182dp"

android:layout_height="wrap_content"

android:layout_weight="0.22" />

android:id="@+id/my_content2"

android:layout_width="182dp"

android:layout_height="wrap_content"

android:layout_weight="0.45" />

追问

这是你从网上抄来的还是个错的吧

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值