没有这样的列_id Android,Java,SQL

时间:2013-11-23 06:58:29

标签: java android sql sqlite

我对Java和Android应用程序都很陌生,几周前刚刚开始,直到现在一切都很顺利,当我因为一个我无法解决的问题而陷入困境几个小时,即使检查了几十个线程。 我正在开发一个非常简单的Android应用程序,它应该可以显示数据库中的电影和年份,可以添加记录,显示所有内容,搜索一年或标题。

我停止了问题
  

没有这样的列_id(代码1):,编译SELECT _id,title,year FROM movies

我将不胜感激任何帮助。

有我的档案:

MainActivity

package com.example.imdbproject;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MainActivity extends Activity {

    ListView moviesList;
    Button searchYear;
    Button searchTitle;
    Button showAll;
    Button addbtn;
    Cursor cursor;
    adapter adapter_ob;
    MySQLiteHelper helper_ob;
    SQLiteDatabase db_ob;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        InitDataBase();

        moviesList = (ListView)findViewById(R.id.listView1);
        searchYear = (Button)findViewById(R.id.buttonYear);
        searchTitle =(Button)findViewById(R.id.buttonTitle);
        addbtn = (Button)findViewById(R.id.buttonAdd);
        showAll = (Button)findViewById(R.id.buttonShowAll);
        adapter_ob = new adapter(this);

        String[] from = { helper_ob.KEY_TITLE, helper_ob.KEY_YEAR };
        int[] to = { R.id.tv_title, R.id.tv_year };
        //PROBLEM
        //cursor = adapter_ob.queryName();
        //PROBLEM
        //SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to, 1);


        /*
        moviesList.setAdapter(cursorAdapter);
        moviesList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {

            Bundle passdata = new Bundle();
            Cursor listCursor = (Cursor) arg0.getItemAtPosition(arg2);
            int nameId = listCursor.getInt(listCursor.getColumnIndex(helper_ob.KEY_ID));

            passdata.putInt("keyid",  nameId);
            Intent passIntent = new Intent(MainActivity.this,EditActivity.class);
            passIntent.putExtras(passdata);
            startActivity(passIntent);
            }
        });
        */




        addbtn.setOnClickListener(new OnClickListener()
        {

            public void onClick(View arg0)
            {
                Intent addsomemoviesIntent = new Intent(MainActivity.this, AddSomeMovies.class);
                startActivity(addsomemoviesIntent);
            }
        });
    }




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


    public void InitDataBase() {
        MySQLiteHelper sqh = new MySQLiteHelper(this);

        SQLiteDatabase sqdb = sqh.getWritableDatabase();

        long result = sqh.addMovie("movietitle", "year");

    }



}

adapter.java

package com.example.imdbproject;

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

public class adapter {

        SQLiteDatabase database_ob;
        MySQLiteHelper openHelper_ob;
        Context context;

        public adapter(Context c)
        {
            context = c;
        }

    public adapter openToRead()
    {
        openHelper_ob = new MySQLiteHelper(context,
                MySQLiteHelper.DATABASE_NAME, null, MySQLiteHelper.DATABASE_VERSION);
        database_ob = openHelper_ob.getWritableDatabase();
        return this;
    }

    public adapter openToWrite()
    {
        openHelper_ob = new MySQLiteHelper(context,
                MySQLiteHelper.DATABASE_NAME, null, MySQLiteHelper.DATABASE_VERSION);
        database_ob = openHelper_ob.getWritableDatabase();
        return this;
    }

    public void Close()
    {
        database_ob.close();
    }

    public long insertDetails(String title, String year)
    {
        ContentValues cv = new ContentValues();
        cv.put(MySQLiteHelper.KEY_TITLE, title);
        cv.put(MySQLiteHelper.KEY_YEAR, year);
        openToWrite();
        long val = database_ob.insert(MySQLiteHelper.TABLE_NAME,  null,  cv);
        Close();
        return val;

    }

    public Cursor queryName()
    {
        String[] cols = { MySQLiteHelper.KEY_ID, MySQLiteHelper.KEY_TITLE,
                MySQLiteHelper.KEY_YEAR };
        openToWrite();
        Cursor c = database_ob.query(MySQLiteHelper.TABLE_NAME,  cols,  null,  null, null, null, null);

        return c;
    }


    public Cursor queryAll(int nameId)
    {
        String[] cols = { MySQLiteHelper.KEY_ID, MySQLiteHelper.KEY_TITLE, MySQLiteHelper.KEY_YEAR };
        openToWrite();
        Cursor c = database_ob.query(MySQLiteHelper.TABLE_NAME, cols, MySQLiteHelper.KEY_ID + "=" + nameId, null,null,null,null); 

        return c;
    }

}

MySQLiteHelper

package com.example.imdbproject;

import java.util.LinkedList;
import java.util.List;

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

public class MySQLiteHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "movie_data.db";
    public static final int DATABASE_VERSION = 1;
    public static final String TABLE_NAME = "movies";
    public static final String KEY_ID = "_id";
    public static final String KEY_TITLE = "title";
    public static final String KEY_YEAR = "year";
    public static final String SCRIPT = "Create table " + TABLE_NAME + " ("
            + KEY_ID + " integer primary key autoincrement, " + KEY_TITLE
            + " text, " + KEY_YEAR + " text);";

    public MySQLiteHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    public MySQLiteHelper(Context context, String name, CursorFactory factory, int version)
    {
        super(context, name, factory, version);

    }

    @Override public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL(SCRIPT);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        db.execSQL("DROP TABLE " + TABLE_NAME);

        onCreate(db);
    }

    public long addMovie(String movietitle, String year){
        ContentValues cv = new ContentValues();
        cv.put(KEY_TITLE, movietitle);
        cv.put(KEY_YEAR, year);

        SQLiteDatabase sd = getWritableDatabase();

        long result = sd.insert(TABLE_NAME,  KEY_TITLE,  cv);
        return result;

    }

}

AddSomeMovies

package com.example.imdbproject;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AddSomeMovies extends Activity {

    adapter adapter;
    MySQLiteHelper helper;
    EditText titleEdit, yearEdit;
    Button addButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_some_movies);

        titleEdit = (EditText)findViewById(R.id.etTitle);
        yearEdit = (EditText)findViewById(R.id.etYear);
        addButton = (Button)findViewById(R.id.button1);
        adapter = new adapter(this);

        addButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0)
        {
            String titleValue = titleEdit.getText().toString();
            String yearValue = yearEdit.getText().toString();
            long val = adapter.insertDetails(titleValue, yearValue);

            finish();

        }


        });

    }

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

}

activity_main.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:background="@color/black"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/buttonShowAll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/buttonTitle"
        android:layout_below="@+id/buttonTitle"
        android:text="@string/showall"
         />

    <Button
        android:id="@+id/buttonYear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/buttonTitle"
        android:layout_alignBottom="@+id/buttonTitle"
        android:layout_alignParentRight="true"
        android:text="@string/yearsearch"
         />

    <Button
        android:id="@+id/buttonTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/titlesearch"
         />

    <Button
        android:id="@+id/buttonAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/buttonShowAll"
        android:layout_alignBottom="@+id/buttonShowAll"
        android:layout_toRightOf="@+id/buttonShowAll"
        android:text="@string/add"
        android:onClick="add" />

    <Button
        android:id="@+id/buttonOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/buttonYear"
        android:layout_below="@+id/buttonYear"
        android:enabled="false"
        android:onClick="ok"
        android:text="@string/ok"
        android:visibility="invisible" />

    <EditText
        android:id="@+id/edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/buttonShowAll"
        android:layout_centerHorizontal="true"
        android:ems="10"
        android:hint="@string/searcheditview" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/buttonShowAll"
        android:layout_below="@+id/edittext" >
    </ListView>

</RelativeLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:orientation="horizontal" >



    <TextView

        android:id="@+id/tv_title"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />



    <TextView

        android:id="@+id/tv_year"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginLeft="20dp" />



</LinearLayout>

2 个答案:

答案 0 :(得分:0)

排序!

至少我是这么认为的。当我将数据库版本从1更改为2时,问题就消失了。所以我猜测现在需要更多关注。

答案 1 :(得分:-1)

尝试以下

 public static final String KEY_ID = BaseColumns._ID;

而不是

 public static final String KEY_ID = "_id";