gpt4 book ai didi

java - Android SQLite 问题 - 表 __ 没有名为 ___ 的列

转载 作者:行者123 更新时间:2023-11-29 07:00:17 24 4
gpt4 key购买 nike

我是一名新开发人员,我遇到了 SQLite 问题。由于某些原因,我无法将两列(table_row_three 和 table_row_four)添加到我的 SQLite 数据表中。当我当前读取数据库文件时,它有一个包含“id”、“table_row_one”和“table_row_two”的表,但缺少“table_row_three”和“table_row_four”。

我的 logcat 中的错误消息是:“(1) 表 database_table 没有名为 table_row_three 的列”。

我真的很困惑为什么“table_row_three”和“table_row_four”列没有显示在我的表中,为什么我的数据表中只出现“id”、“table_row_one”和“table_row_two”。

这是我的数据库类:

DatabaseHandler.java


import java.util.ArrayList;

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;



public class DatabaseHandler
{

Context context;


private SQLiteDatabase db;


private final String DB_NAME = "database_name";
private final int DB_VERSION = 2;


private final String TABLE_NAME = "database_table";
private final String TABLE_ROW_ID = "id";

private final String TABLE_ROW_ONE = "table_row_one";
private final String TABLE_ROW_TWO = "table_row_two";
private final String TABLE_ROW_THREE = "table_row_three";
private final String TABLE_ROW_FOUR = "table_row_four";

public DatabaseHandler(Context context)
{
this.context = context;

// create or open the database
CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
this.db = helper.getWritableDatabase();
}


public void addRow(String rowStringOne, String rowStringTwo, String rowStringThree, String rowStringFour)
{
// this is a key value pair holder used by android's SQLite functions
ContentValues values = new ContentValues();
//TABLE_ROW_ONE = Title
//TABLE_ROW_TWO = Location
//TABLE_ROW_THREE = Notes
//TABLE_ROW_FOUR = Picture
values.put(TABLE_ROW_ONE, rowStringOne);
values.put(TABLE_ROW_TWO, rowStringTwo);
values.put(TABLE_ROW_THREE, rowStringThree);
values.put(TABLE_ROW_FOUR, rowStringFour);

// ask the database object to insert the new data
try{db.insert(TABLE_NAME, null, values);}
catch(Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}




public void deleteRow(long rowID)
{
// ask the database manager to delete the row of given id
try {db.delete(TABLE_NAME, TABLE_ROW_ID + "=" + rowID, null);}
catch (Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}

public void updateRow(long rowID, String rowStringOne, String rowStringTwo, String rowStringThree, String rowStringFour)
{
ContentValues values = new ContentValues();
values.put(TABLE_ROW_ONE, rowStringOne);
values.put(TABLE_ROW_TWO, rowStringTwo);
values.put(TABLE_ROW_THREE, rowStringThree);
values.put(TABLE_ROW_FOUR, rowStringFour);

try {db.update(TABLE_NAME, values, TABLE_ROW_ID + "=" + rowID, null);}
catch (Exception e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}
}


public ArrayList<Object> getRowAsArray(long rowID)
{

ArrayList<Object> rowArray = new ArrayList<Object>();
Cursor cursor;

try
{

cursor = db.query
(
TABLE_NAME,
new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO, TABLE_ROW_THREE, TABLE_ROW_FOUR },
TABLE_ROW_ID + "=" + rowID,
null, null, null, null, null
);


cursor.moveToFirst();


if (!cursor.isAfterLast())
{
do
{
rowArray.add(cursor.getLong(0));
rowArray.add(cursor.getString(1));
rowArray.add(cursor.getString(2));
rowArray.add(cursor.getString(3));
rowArray.add(cursor.getString(4));
}
while (cursor.moveToNext());
}


cursor.close();
}
catch (SQLException e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}

// return the ArrayList containing the given row from the database.
return rowArray;
}






public ArrayList<ArrayList<Object>> getAllRowsAsArrays()
{

ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();


Cursor cursor;

try
{
cursor = db.query(
TABLE_NAME,
new String[]{TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO, TABLE_ROW_THREE, TABLE_ROW_FOUR},
null, null, null, null, null
);

cursor.moveToFirst();

if (!cursor.isAfterLast())
{
do
{
ArrayList<Object> dataList = new ArrayList<Object>();

dataList.add(cursor.getLong(0));
dataList.add(cursor.getString(1));
dataList.add(cursor.getString(2));
dataList.add(cursor.getString(3));
dataList.add(cursor.getString(4));
dataArrays.add(dataList);
}
while (cursor.moveToNext());
}
}
catch (SQLException e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}

return dataArrays;
}








private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
{
public CustomSQLiteOpenHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db)
{

String newTableQueryString = "create table " +
TABLE_NAME +
" (" +
TABLE_ROW_ID + " integer primary key autoincrement not null," +
TABLE_ROW_ONE + " text," +
TABLE_ROW_TWO + " text" +
TABLE_ROW_THREE + " text" +
TABLE_ROW_FOUR + " text" +
");";
db.execSQL(newTableQueryString);
}


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

}
}

这是我的测试类:

public class DatabaseExampleActivity extends Activity {
EditText textFieldOne, textFieldTwo, textFieldThree, textFieldFour,
idField,
updateIDField, updateTextFieldOne, updateTextFieldTwo,updateTextFieldThree, updateTextFieldFour;

Button addButton, deleteButton, retrieveButton, updateButton;

TableLayout dataTable;

DatabaseHandler db;



@Override
public void onCreate(Bundle savedInstanceState) {
// this try catch block returns better error reporting to the log
try {
// Android specific calls
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_databaseexample);

// create the database manager object
db = new DatabaseHandler(this);

// create references and listeners for the GUI interface
setupViews();

// make the buttons clicks perform actions
addButtonListeners();

// load the data table
updateTable();
} catch (Exception e) {
Log.e("ERROR", e.toString());
e.printStackTrace();
}
}


private void setupViews() {
// THE DATA TABLE
dataTable = (TableLayout) findViewById(R.id.data_table);

// THE DATA FORM FIELDS
textFieldOne = (EditText) findViewById(R.id.text_field_one);
textFieldTwo = (EditText) findViewById(R.id.text_field_two);
textFieldThree = (EditText) findViewById(R.id.text_field_three);
textFieldFour = (EditText) findViewById(R.id.text_field_four);
idField = (EditText) findViewById(R.id.id_field);
updateIDField = (EditText) findViewById(R.id.update_id_field);
updateTextFieldOne = (EditText) findViewById(R.id.update_text_field_one);
updateTextFieldTwo = (EditText) findViewById(R.id.update_text_field_two);
updateTextFieldThree = (EditText) findViewById(R.id.update_text_field_three);
updateTextFieldFour = (EditText) findViewById(R.id.update_text_field_four);

addButton = (Button) findViewById(R.id.add_button);
deleteButton = (Button) findViewById(R.id.delete_button);
retrieveButton = (Button) findViewById(R.id.retrieve_button);
updateButton = (Button) findViewById(R.id.update_button);
}



private void addButtonListeners() {
addButton.setOnClickListener
(
new View.OnClickListener() {
@Override
public void onClick(View v) {
addRow();
}
}
);

deleteButton.setOnClickListener
(
new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteRow();
}
}
);

updateButton.setOnClickListener
(
new View.OnClickListener() {
@Override
public void onClick(View v) {
updateRow();
}
}
);

retrieveButton.setOnClickListener
(
new View.OnClickListener() {
@Override
public void onClick(View v) {
retrieveRow();
}
}
);

}


private void addRow() {
try {
// ask the database manager to add a row given the two strings
db.addRow
(
textFieldOne.getText().toString(),
textFieldTwo.getText().toString(),
textFieldThree.getText().toString(),
textFieldFour.getText().toString()

);

// request the table be updated
updateTable();

// remove all user input from the Activity
emptyFormFields();
} catch (Exception e) {
Log.e("Add Error", e.toString());
e.printStackTrace();
}
}


private void deleteRow() {
try {
// ask the database manager to delete the row with the give rowID.
db.deleteRow(Long.parseLong(idField.getText().toString()));

// request the table be updated
updateTable();

// remove all user input from the Activity
emptyFormFields();
} catch (Exception e) {
Log.e("Delete Error", e.toString());
e.printStackTrace();
}
}


private void retrieveRow() {
try {
// The ArrayList that holds the row data
ArrayList<Object> row;
// ask the database manager to retrieve the row with the given rowID
row = db.getRowAsArray(Long.parseLong(updateIDField.getText().toString()));

// update the form fields to hold the retrieved data
updateTextFieldOne.setText((String) row.get(1));
updateTextFieldTwo.setText((String) row.get(2));
updateTextFieldThree.setText((String) row.get(3));
updateTextFieldFour.setText((String) row.get(4));

} catch (Exception e) {
Log.e("Retrieve Error", e.toString());
e.printStackTrace();
}
}

private void updateRow() {
try {
// ask the database manager to update the row based on the information
// found in the corresponding user entry fields
db.updateRow
(
Long.parseLong(updateIDField.getText().toString()),
updateTextFieldOne.getText().toString(),
updateTextFieldTwo.getText().toString(),
updateTextFieldThree.getText().toString(),
updateTextFieldFour.getText().toString()
);

updateTable();

emptyFormFields();
} catch (Exception e) {
Log.e("Update Error", e.toString());
e.printStackTrace();
}
}


private void emptyFormFields() {
textFieldOne.setText("");
textFieldTwo.setText("");
textFieldThree.setText("");
textFieldFour.setText("");
idField.setText("");
updateIDField.setText("");
updateTextFieldOne.setText("");
updateTextFieldTwo.setText("");
updateTextFieldThree.setText("");
updateTextFieldFour.setText("");
}


private void updateTable() {
// delete all but the first row. remember that the count
// starts at one and the index starts at zero
while (dataTable.getChildCount() > 1) {
// while there are at least two rows in the table widget, delete
// the second row.
dataTable.removeViewAt(1);
}

// collect the current row information from the database and
// store it in a two dimensional ArrayList
ArrayList<ArrayList<Object>> data = db.getAllRowsAsArrays();

// iterate the ArrayList, create new rows each time and add them
// to the table widget.
for (int position = 0; position < data.size(); position++) {
TableRow tableRow = new TableRow(this);

ArrayList<Object> row = data.get(position);

TextView idText = new TextView(this);
idText.setText(row.get(0).toString());
tableRow.addView(idText);

TextView textOne = new TextView(this);
textOne.setText(row.get(1).toString());
tableRow.addView(textOne);

TextView textTwo = new TextView(this);
textTwo.setText(row.get(2).toString());
tableRow.addView(textTwo);


TextView textThree = new TextView(this);
textThree.setText(row.get(3).toString());
tableRow.addView(textThree);


TextView textFour = new TextView(this);
textFour.setText(row.get(4).toString());
tableRow.addView(textFour);


dataTable.addView(tableRow);
}
}

最佳答案

创建语句中缺少“,”:

String newTableQueryString = "create table " +
TABLE_NAME +
" (" +
TABLE_ROW_ID + " integer primary key autoincrement not null," +
TABLE_ROW_ONE + " text," +
TABLE_ROW_TWO + " text," +
TABLE_ROW_THREE + " text," +
TABLE_ROW_FOUR + " text" +
");";

关于java - Android SQLite 问题 - 表 __ 没有名为 ___ 的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27309066/

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