Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的sqlite示例程序_Android_Sqlite - Fatal编程技术网

android中的sqlite示例程序

android中的sqlite示例程序,android,sqlite,Android,Sqlite,我是数据库概念的新手,特别是我需要相关的数据库概念。我想要一个使用android的sqlite数据库的示例。我浏览了android开发人员网站,但发现很少关于数据库示例的内容。。 由于我对数据库概念还不熟悉,有人能给我推荐一些好的资源来理解create()、insert()、Query()……等等吗?如果我能得到任何与android一起工作的示例程序,那就更好了 提前感谢很简单,请按照下面的代码操作。。。 创建3个名为的类,例如- 1.我使用了Constants接口,参考了完整的代码并找到了它的

我是数据库概念的新手,特别是我需要相关的数据库概念。我想要一个使用android的sqlite数据库的示例。我浏览了android开发人员网站,但发现很少关于数据库示例的内容。。 由于我对数据库概念还不熟悉,有人能给我推荐一些好的资源来理解create()、insert()、Query()……等等吗?如果我能得到任何与android一起工作的示例程序,那就更好了


提前感谢

很简单,请按照下面的代码操作。。。 创建3个名为的类,例如- 1.我使用了Constants接口,参考了完整的代码并找到了它的用途

          package com.mypackage.quaddeals;

import android.net.Uri;
import android.provider.BaseColumns;

public interface Constants extends BaseColumns {
    public static final String TABLE_NAME = "user_deal";

    public static final String AUTHORITY = "com.mypackage.quaddeals";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
            + "/" + TABLE_NAME);

    public static final String TITLE = "title";
    public static final String CITYID = "cityid";

}
2.BruPress数据类

package com.mypackage.quaddeals;

import static android.provider.BaseColumns._ID;

import static com.mypackage.quaddeals.Constants.CITYID;
import static com.mypackage.quaddeals.Constants.TITLE;
import static com.mypackage.quaddeals.Constants.TABLE_NAME;

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

public class Bru_Press_Data extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "QuadDeals.db";
    public static final int DATABASE_VERSION = 1;

    /** Create a helper object for the Events database */
    public Bru_Press_Data(Context ctx) {
        super(ctx, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + _ID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT," + TITLE
                    + " TEXT UNIQUE," + CITYID + " TEXT);");            
        } catch (Exception e) {
            Log.v("Bru_Press_Data", "exception in table created");
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

}
3.提供者类

package com.mypackage.quaddeals;

import static android.provider.BaseColumns._ID;

import static com.mypackage.quaddeals.Constants.CONTENT_URI;
import static com.mypackage.quaddeals.Constants.AUTHORITY;
import static com.mypackage.quaddeals.Constants.TABLE_NAME;

import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class Bru_Press_Provider extends ContentProvider {
    private static final int QUADUSER = 1;
    private static final int QUADUSER_ID = 2;
    /** The MIME type of a directory of events */
    private static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.example.brown";

    /** The MIME type of a single event */
    private static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.example.brown";
    private static final String CONTENT_TYPE1 = "vnd.android.cursor.dir/vnd.example.brown";

    /** The MIME type of a single event */
    private static final String CONTENT_ITEM_TYPE1 = "vnd.android.cursor.item/vnd.example.brown";
//    private static final String CONTENT_TYPE2 = "vnd.android.cursor.dir/vnd.example.brown";

    /** The MIME type of a single event */
//  private static final String CONTENT_ITEM_TYPE2 = "vnd.android.cursor.item/vnd.example.brown";
    private Bru_Press_Data news;
    private UriMatcher uriMatcher;

    private Cursor cursor;
    private SQLiteDatabase db;
    String[] str;

    @Override
    public boolean onCreate() {

        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY, "user_deal", QUADUSER);
        uriMatcher.addURI(AUTHORITY, "user_deal/#", QUADUSER_ID);

        news = new Bru_Press_Data(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String orderBy) {
        SQLiteDatabase db;
        Cursor cursor = null;
        if (uriMatcher.match(uri) == QUADUSER_ID) {
            long id = Long.parseLong(uri.getPathSegments().get(1));
            selection = appendRowId(selection, id);
        }
        if (uriMatcher.match(uri) == QUADUSER) {
            // Get the database and run the query
            db = news.getReadableDatabase();
            cursor = db.query(TABLE_NAME, projection, selection, selectionArgs,
                    null, null, orderBy);
        }
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)) {
        case QUADUSER:
            return CONTENT_TYPE;
        case QUADUSER_ID:
            return CONTENT_ITEM_TYPE;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {

        SQLiteDatabase db = news.getWritableDatabase();
        Uri newUri = null;
        long id = -1;
        // Validate the requested uri

        if (uriMatcher.match(uri) == QUADUSER) {
            try {

                id = db.insertOrThrow(TABLE_NAME, null, values);
                // Notify any watchers of the change
                newUri = ContentUris.withAppendedId(CONTENT_URI, id);
                getContext().getContentResolver().notifyChange(newUri, null);

            } catch (NullPointerException e) {
                    Log.v("Error","OnInsert"+e);
            }
        } 
        else if (uriMatcher.match(uri) != QUADUSER) {
            throw new IllegalArgumentException("Unknown URI bru press " + uri);
        }

        return newUri;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = news.getWritableDatabase();
        int count;
        long id;        
        Log.v("Delete",selection);
        switch (uriMatcher.match(uri)) {

        case QUADUSER:
            count = db.delete(TABLE_NAME, selection, selectionArgs);
            break;
        case QUADUSER_ID:
            id = Long.parseLong(uri.getPathSegments().get(1));
            count = db.delete(TABLE_NAME, appendRowId(selection, id),
                    selectionArgs);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        SQLiteDatabase db = news.getWritableDatabase();
        int count;
        long id;        
        switch (uriMatcher.match(uri)) {
        case QUADUSER:
            count = db.update(TABLE_NAME, values, selection, selectionArgs);
            break;
        case QUADUSER_ID:
            id = Long.parseLong(uri.getPathSegments().get(1));
            count = db.update(TABLE_NAME, values, appendRowId(selection, id),
                    selectionArgs);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }

        // Notify any watchers of the change
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }

    /** Append an id test to a SQL selection expression */
    private String appendRowId(String selection, long id) {
        return _ID
                + "="
                + id
                + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')'
                        : "");
    }

}
注意:您应该在清单文件中定义这个提供程序类,如

在另一个类中,我可以将数据插入并检索到DB…如下

class A extends Activity{

  {

 addevent(cityname,cityid);  // to insert
 }

  private void addevent(String a1, String a2) {
        try {
            ContentValues values = new ContentValues();         
            values.put(Constants.TITLE, a1);
            values.put(Constants.CITYID, a2);   
            getContentResolver().insert(Constants.CONTENT_URI, values);

        } catch (Exception e) {
            Log.v("Error",""+e);
        }
    }
从数据库中检索

Cursor c = getContentResolver().query(Constants.CONTENT_URI,
                    null, null, null,null);
            int a = c.getCount();
            Log.v("Count",""+a);

               int columnIndex  =  c.getColumnIndex(Constants.TITLE);
               int columnIndex2 =  c.getColumnIndex(Constants.CITYID);
                if (c.moveToNext()) {
                  cityName = c.getString(columnIndex);
                  city_id= c.getString(columnIndex2);
                }

  }

试试这样的东西……它将帮助您参考这篇文章,这是一篇好文章。请阅读下面的教程,在sqlite+1中访问数据,超过17k的视图仍然没有投票!嗨,让我们试着使用下面的链接来做同样的事情,这里的一切都是基于对象模型的,我们使用ORMAN---ORMLite来表示相同的google it。ORMAN是ORMLite的增强版