我创建了一个数据库应用程序,允许用户搜索电影数据库。我的理解是,当点击搜索按钮时,它应该调用搜索方法来检查数据库条目并返回与搜索词匹配的条目,但是点击按钮会使应用程序崩溃,任何帮助和解释都将不胜感激。确认正在创建数据库也很棒,谢谢
主要活动
public class MainActivity extends ActionBarActivity {
public final static String FILM = "com.mnt.filmapp6.FILM";
protected SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAddFilm = (Button) findViewById(R.id.add_film);
Button btnFavourite = (Button) findViewById(R.id.favourites);
btnAddFilm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity2.class);
startActivity(intent);
}
});
btnFavourite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity3.class);
startActivity(intent);
}
}
);
}
public void search(View v){
EditText edtSearch = (EditText)findViewById(R.id.edtSearch);
String search = "%" + edtSearch.getText().toString() + "%";
db = new DbHelper(this).getReadableDatabase();
String[] tblColumns = {"*"};
String where = "film LIKE ? OR actor LIKE ? OR actor2 LIKE ? OR director LIKE ?";
String[] whereArgs = {search, search, search, search};
Cursor results = db.query("FILMTABLE", tblColumns, where, whereArgs, null, null, null);
films(results);
}
public void films (Cursor c){
int titleIndex = c.getColumnIndex("film");
int directorIndex = c.getColumnIndex("director");
int idIndex = c.getColumnIndex("id");
String title = c.getString(titleIndex);
String director = c.getString(directorIndex);
int filmID = c.getInt(idIndex);
TextView txt = new TextView(getApplicationContext());
txt.setId(filmID);
txt.setText(title + ", " + director);
txt.setTextColor(Color.BLACK);
txt.setTextSize(15);
//txt.setText(ScrollView);
}
@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);
}
}
Dbhelper
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
FILM + "TEXT NOT NULL, " +
ACTOR + "TEXT NOT NULL, " +
ACTOR2 + "TEXT NOT NULL, " +
DIRECTOR + "TEXT NOT NULL, " +
DESCRIPTION + "TEXT NOT NULL);"
);
Cursor countRows = db.rawQuery("SELECT count(*) FROM FILMTABLE", null);
countRows.moveToFirst();
int NumRows = countRows.getInt(0);
countRows.close();
if (NumRows == 0) {
ContentValues values = new ContentValues();
values.put("film", "Wolf of Wall Street");
values.put("actor", "Leonardo Dicaprio");
values.put("actor2", "Jonah Hill");
values.put("director", "Martin Scorses");
values.put("description", "True story of New York stockbroker Jordan Belfort. From the American dream to corporate greed, Belfort goes from penny stocks and righteousness to IPOs and a life of corruption in the late 80's.");
db.insert("FILMTABLE", null, values);
values.clear();
values.put("film", "Captain Philips");
values.put("actor", "Tom Hanks");
values.put("actor2", "Catherine Keener");
values.put("director", "Paul Greengrass");
values.put("description", "True story, Captain Phillips is a multi-layered examination of the 2009 hijacking of the U.S. container ship Maersk Alabama by a crew of Samali pirates.");
db.insert("FILMTABLE", null, values);
values.clear();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
}
主要活动xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/film_app" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/textView" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/search"
android:id="@+id/search"
android:layout_below="@+id/edtSearch"
android:layout_centerHorizontal="true"
android:onClick="search"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add_film"
android:id="@+id/add_film"
android:layout_below="@+id/favourites"
android:layout_alignLeft="@+id/search"
android:layout_alignStart="@+id/search"
android:onClick="add"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/favourites"
android:id="@+id/favourites"
android:layout_below="@+id/search"
android:layout_centerHorizontal="true"
android:onClick="favourite"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edtSearch"
android:hint="Enter Film/Actor/Director"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:id="@+id/resultLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
logcat的
03-04 12:16:53.829 1879-1879/com.mnt.filmapp6 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mnt.filmapp6, PID: 1879
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.database.sqlite.SQLiteException: no such column: film (code 1): , while compiling: SELECT * FROM FILMTABLE WHERE film LIKE ? OR actor LIKE ? OR actor2 LIKE ? OR director LIKE ?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
at com.mnt.filmapp6.MainActivity.search(MainActivity.java:58)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:3)
错误在于:
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
FILM + "TEXT NOT NULL, " +
ACTOR + "TEXT NOT NULL, " +
ACTOR2 + "TEXT NOT NULL, " +
DIRECTOR + "TEXT NOT NULL, " +
DESCRIPTION + "TEXT NOT NULL);"
);
应该是这样的:
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
FILM + " TEXT NOT NULL, " +
ACTOR + " TEXT NOT NULL, " +
ACTOR2 + " TEXT NOT NULL, " +
DIRECTOR + " TEXT NOT NULL, " +
DESCRIPTION + " TEXT NOT NULL);"
);
答案 1 :(得分:1)
引起:android.database.sqlite.SQLiteException:没有这样的列: 电影(代码1):,同时编译:SELECT * FROM FILMTABLE WHERE电影 喜欢 ?或演员喜欢?还是演员2喜欢?或导演LIKE?
检查FILMTABLE
列的数据库表film
。我认为数据库表或SQL语法中没有film
列是错误的。
尝试使用任何客户端首先在数据库上运行查询,然后在项目中使用它。
答案 2 :(得分:0)
Caused by: android.database.sqlite.SQLiteException: no such column: film (code 1): , while compiling: SELECT * FROM FILMTABLE WHERE film LIKE ? OR actor LIKE ? OR actor2 LIKE ? OR director LIKE ?
检查film
列的表和SQL语法。
在Stack Overflow中发布之前,请对Logs
进行详细分析。