android—sqlite

有个问题没解决:
怎样让同名的书籍无法插入到表,我给name 加了unique属性,但是同名书籍仍能插入
练习:
 
 
package com.app.cceasy.sqlitepractice;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {
    TextView textView;
    SQLiteDatabase sqLiteDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.id_text_show);

        MySqliteOpenHelper mySqliteOpenHelper = new MySqliteOpenHelper(this,"mydata",null,1);
        sqLiteDatabase = mySqliteOpenHelper.getReadableDatabase();

    }


    public void addData(View v){
        sqLiteDatabase.execSQL("insert into booktable(name,author,pages,price) values(?,?,?,?)",
                new String[]{"The Da Vinci Code", "Dan Brown", "454", "16.96"});
        sqLiteDatabase.execSQL("insert into booktable(name,author,pages,price) values(?,?,?,?)",
                new String[]{"TCP/IP", "Richard", "300", "40"});
    }

    public void update(View v){
        sqLiteDatabase.execSQL("update booktable set price = ? where pages > ?", new String[]{"20", "300"});

    }

    public void deleteData(View v){
        sqLiteDatabase.execSQL("delete from booktable where price >?", new String[]{"10"});
    }

    public void queryData(View v){
        textView.setText("id name author pages price");

        Cursor cursor = sqLiteDatabase.rawQuery("select * from booktable",null);

        if (cursor.moveToFirst()) {

            do {

                // 遍历Cursor对象,取出数据并打印
                int id = cursor.getInt(cursor.getColumnIndex("id"));

                String name = cursor.getString(cursor.getColumnIndex("name"));
                String author = cursor.getString(cursor.getColumnIndex("author"));
                int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                double price = cursor.getDouble(cursor.getColumnIndex("price"));


                textView.append("\n" +id+" "+ name + " " + author + " " + pages + " " + price);
            } while (cursor.moveToNext());
        }
        cursor.close();

    }
}


class MySqliteOpenHelper extends SQLiteOpenHelper{

    public static final String CREATE_BOOK = "create table booktable ("
            + "id integer primary key autoincrement, "
            + "author text , "
            + "price real, "
            + "pages integer, "
            + "name text UNIQUE,"
            + "category_id integer)";

    public static final String CREATE_CATEGORY = "create table Category ("
            + "id integer primary key autoincrement, "
            + "category_name text, "
            + "category_code integer)";


    private Context mContext;
    public MySqliteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory
            factory, int version) {
        super(context, name, factory, version);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_BOOK);
        db.execSQL(CREATE_CATEGORY);
        Toast.makeText(mContext,"create table successful",Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        switch (oldVersion){
            case 1:
                db.execSQL(CREATE_CATEGORY);
            case 2:
                db.execSQL("alter table Book add column category_id integer");
            default:
        }

    }
}
posted @ 2015-08-13 15:13  cuiz_book  阅读(182)  评论(0编辑  收藏  举报