gpt4 book ai didi

java - 从 SQLite 数据库提取数据 (Android)

转载 作者:行者123 更新时间:2023-12-01 11:43:17 26 4
gpt4 key购买 nike

我能够在主 Activity 中将数据写入数据库,但在其他 Activity 中读取和显示数据时遇到问题。我正在使用带有“addData”方法(根据调试器工作正常)和 showData 方法(这就是问题所在)的 SQLHelperDatabase 类。在主 Activity 中输入数据后,我希望能够按下发送消息按钮并将数据显示在新 Activity 中。我已经在这个问题上坚持了一个星期了,我一直在搜索 WWW,每个教程都说我的代码应该可以工作。请有人能帮助我了解这个令人沮丧的磨难的真相。

package com.doors.waynderful.myapp;

import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class DisplayMessageActivity extends ActionBarActivity {
DatabaseHelper db;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new DatabaseHelper(this);
Cursor cursor = db.showData();
cursor.moveToFirst();

String nam = cursor.getString(cursor.getColumnIndex(DatabaseHelper.THE_TABLE_COLUMN_NAME));

if (!cursor.isClosed()){
cursor.close();
}




TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(nam);

setContentView(textView);
//setContentView(R.layout.activity_display_message);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_display_message, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

下面是 SQLHelperDatabase。

package com.doors.waynderful.myapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseHelper extends SQLiteOpenHelper
{
public static final String THE_DATABASE = "theDatabase";
public static final String THE_TABLE = "infoTable";
public static final String THE_TABLE_COLUMN_ID = "id";
public static final String THE_TABLE_COLUMN_NAME = "name";
public static final String THE_TABLE_COLUMN_DETAILS = "details";

DatabaseHelper(Context context)
{
super(context,THE_DATABASE, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " +THE_TABLE+"(id integer primary key, name text, details text) ");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + THE_TABLE + "");

onCreate(db);
}

public void addData(String nameIn, String detailsIn)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues data = new ContentValues();

data.put("name", nameIn);
data.put("details", detailsIn);

db.insert(THE_TABLE,null,data);

}

public Cursor showData()
{
SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor;
cursor = db.rawQuery("select * from "+THE_TABLE+"", null);
return cursor;

}
}

下面是MainActivity

package com.doors.waynderful.myapp;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;



public class MainActivity extends ActionBarActivity {

public final static String EXTRA_MESSAGE = "com.doors.waynderful.myapp.MESSAGE";
DatabaseHelper myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

public void sendMessage(View view){
myDb = new DatabaseHelper(this);


Intent intent = new Intent(this,DisplayMessageActivity.class);

EditText editText = (EditText) findViewById(R.id.edit_name);
EditText editText1 = (EditText) findViewById(R.id.edit_details);

String name = editText.getText().toString();
String details = editText1.getText().toString();

myDb.addData(name, details);
//intent.putExtra(EXTRA_MESSAGE,message);

startActivity(intent);

}
}

LOGCAT 显示

03-31 09:40:31.100 1351-1351/com.doors.waynderful.myapp E/SQLiteLog:(1)没有这样的表:infoTable

最佳答案

我不知道为什么你认为数据库中必须有一行 id = 1

如果要查找具有特定名称的行,请使用选择具有该名称的行的 SQL 语句。

如果要列出所有行,请选择所有行。

但是插入一行,然后假设您知道它的 id 是行不通的。

此外,您应该使用使用布局的代码(当前已注释掉),而不是创建新的 TextView 并将其设置为内容 View 。

最后,您似乎没有任何实际在数据库中创建表的代码?

关于java - 从 SQLite 数据库提取数据 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29336923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com