你好我对android编程很新,并且因为错误而被困了好几个小时。应该发生的是,在它加载了我的表中的所有引号之后,如果我点击一个我的它应该将id传递给一个新的片段并将其再次加载到一个片段上。但最终发生的是当我点击引号时,我收到一个错误,它指向我的DatabaseHelper类中的getItemID方法。我得到的错误是我一直在 -
android.database.sqlite.SQLiteException: near "SELECTIDFROMQuote_TableWHEREQuote": syntax error (code 1): , while compiling: SELECTIDFROMQuote_TableWHEREQuote = 'Play it, Sam. Play 'As Time Goes By.'
然后指向 - getItemID(DatabaseHelper.java:78)
任何帮助都会有所帮助。
(这是包含listFragments和favoriteFragment的活动)
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class FavoriteActivity extends AppCompatActivity {
final String LOG_TAG = FavoriteActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite);
Log.e(LOG_TAG, "youre now in FavoriteActivity ");
FavoriteListFragment newFragment = new FavoriteListFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.favorite_Container, newFragment);
// Commit the transaction
transaction.commit();
}
}
(这是点击listview引用后加载的片段) (这是我在ID中遇到问题的地方)
public class FavoriteFragment extends Fragment {
DatabaseHelper mDatabaseHelper;
private String selectedQuote;
private int selectedID;
private TextView savedQuoteView;
final String LOG_TAG = FavoriteFragment.class.getSimpleName();
public FavoriteFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e(LOG_TAG, "youre now in FavoriteFragment ");
Bundle openBundle = getArguments();
selectedID = openBundle.getInt("ID", -1);
String selectedQuote = openBundle.getString("quote", "empty");
mDatabaseHelper = new DatabaseHelper(getActivity());
View view = inflater.inflate(R.layout.fragment_favorite, container, false);
Button deleteBtn = (Button) view.findViewById(R.id.deleteButton);
deleteBtn.setOnClickListener(new AdapterView.OnClickListener() {
@Override
public void onClick(View v) {
// Log.e(LOG_TAG, "selectedID : " + selectedID + " selectedQuote : " + selectedQuote);
mDatabaseHelper.deleteQuote(selectedID, selectedQuote);
savedQuoteView.setText("");
FavoriteListFragment favoriteListFragment = new FavoriteListFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.favorite_Container, favoriteListFragment);
// Commit the transaction
transaction.commit();
Toast.makeText(getActivity(), "removed from database", Toast.LENGTH_SHORT).show();
}
});
savedQuoteView = (TextView) view.findViewById(R.id.savedQuote);
savedQuoteView.setText(selectedQuote);
return view;
}}
(这是显示我的表的listFragment)
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class FavoriteListFragment extends Fragment {
private static final String LOG_TAG = "FavoriteListFragment";
private ListView listView;
DatabaseHelper mDatabaseHelper;
public FavoriteListFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
// final String LOG_TAG = FavoriteActivity.class.getSimpleName();
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_favorite_list, container, false);
listView = view.findViewById(android.R.id.list);
mDatabaseHelper = new DatabaseHelper(getActivity());
//Log.e(LOG_TAG, "mDataBaseHelper: " + mDatabaseHelper);
Log.e(LOG_TAG, "you're now in FavoriteListFragment ");
// Log.e(LOG_TAG, "this is your list view " + listView);
populateListView();
return view;
}
private void populateListView() {
Log.d(LOG_TAG, "youre in populateListView");
//get the data and append to a list
Cursor data = mDatabaseHelper.getAllQuotes();
ArrayList<String> listData = new ArrayList<>();
while (data.moveToNext()) {
//get the value from the database in column 1
//then add it to the ArrayList
listData.add(data.getString(1));
}
//create the list adapter and set the adapter
ListAdapter adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, listData);
listView.setAdapter(adapter);
//set an onItemClickListener to the ListView
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String quote = adapterView.getItemAtPosition(i).toString();
Cursor data = mDatabaseHelper.getItemID(quote); //get the id associated with that name
int itemID = -1;
while (data.moveToNext()) {
itemID = data.getInt(0);
Log.d(LOG_TAG, "onItemClick: You Clicked on " + quote);
}
if (itemID > -1) {
Log.d(LOG_TAG, "onItemClick: The ID is: " + itemID);
FavoriteFragment newFragment = new FavoriteFragment();
Bundle args = new Bundle();
args.putInt("ID", itemID);
args.putString("quote", quote);
newFragment.setArguments(args);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.favorite_Container, newFragment);
transaction.commit();
} else {
Toast toast = Toast.makeText(getActivity(), "No ID associated with that name", Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
}
(这是我正在使用的处理程序)
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String LOG_TAG = "DatabaseHelper";
private static final String TABLE_NAME = "Quote_Table";
private static final String COL0 = "ID";
private static final String COL1 = "Quote";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL1 + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int v1, int v2) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item) {
SQLiteDatabase dataBase = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL1, item);
long result = dataBase.insert(TABLE_NAME, null, values);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
/**
* Returns all the data from database
*/
public Cursor getAllQuotes() {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Returns only the ID that matches the name passed in
*/
public Cursor getItemID(String quote) {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL0 + " FROM " + TABLE_NAME +
" WHERE " + COL1 + " = '" + quote + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Delete from database
*
* @param id
* @param quote
*/
public void deleteQuote(int id, String quote) {
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + TABLE_NAME + " WHERE "
+ COL0 + " = '" + id + "'" + " AND " + COL1 + " = '" + quote + "'";
Log.d(LOG_TAG, "deleteName: query: " + query);
Log.d(LOG_TAG, "deleteName: Deleting " + quote + " from database.");
db.execSQL(query);
}
}
(如果你想看到logcat说的话):
randomquote.hfad.com.randomquote.DatabaseHelper.getItemID(DatabaseHelper.java:71)
at
randomquote.hfad.com.randomquote.FavoriteListFragment$1.onItemClick(FavoriteListFragment.java:72)
答案 0 :(得分:0)
我的猜测是,引号包含未正确转义的字符(猜测时引用)。我建议尝试使用为您逃避的查询便捷方法。
因此尝试使用以下内容: -
public Cursor getItemID(String quote) {
SQLiteDatabase db = this.getWritableDatabase();
String[] columns = new String[]{COL0};
String whereclause = COL1+"=?";
String[] whereargs = new String[]{quote};
return db.query(TABLE_NAME,columns,whereclause,whereargs,null,null,null);
}