我是一個新的開發人員,我有一個SQLite問題。某些原因我無法將兩列(table_row_three和table_row_four)添加到我的SQLite數據表中。當我讀取數據庫文件時,它有一個帶有「id」,「table_row_one」和「table_row_two」的表格,但缺少「table_row_three」和「table_row_four」。Android的SQLite問題 - 表__沒有列名___
我的logcat中的錯誤消息是:「(1)table database_table has no column named 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);
}
}
你在創建表語句中缺少','。 – 2014-12-05 05:06:28