如何使用游标适配器在android中创建简单的SQLite数据库?

时间:2017-03-31 06:39:18

标签: listview android-studio android-sqlite android-custom-view

嗨,我刚刚进入android编程我想在android应用程序中使用sqlite数据库来处理一些与数据库相关的东西。我正在开发应用程序,就像它将从家里扫描一些wifi设备到那里供应商ID应该在列表视图中显示按下列表中的任何项目它应该发送一些udp按摩到该受尊重的设备得到响应udp按摩它应该再创建一个listview为那个回应。我已经实现了Udp相关的东西和扫描wifi设备相关的东西,但只有这是剩下的是获取设备我需要存储该设备进一步udp相关的东西和维护数据库可以任何人有最好的建议。

lass IP_Channal_DB extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "note.db";
    private static final String Table_NAME = "Ip_channal_Table";
    private static final String Col_1 = "ID";
    private static final String Col_2 = "IP_Address";
    private static final String Col_3 = "Flag";
    private static final String Col_4 = "Channal_1";
    private static final String Col_5 = "Channal_2";
    private static final String Col_6 = "Channal_3";
    private static final String Col_7 = "Channal_4";

    private static final int SCHEMA_VERSION = 1;

    private static final String DATABASE_CREATE = "CREATE TABLE " + Table_NAME + " (" + Col_1 + " integer PRIMARY KEY autoincrement," + Col_2 + "TEXT," + Col_3 + "TEXT," + Col_4 + "TEXT," + Col_5 + "TEXT," + Col_6 + "TEXT," + Col_7 + "TEXT,)";


    public IP_Channal_DB(Context context) {

        super(context, DATABASE_NAME, null, SCHEMA_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.v("MainActivity","creating table");
        db.execSQL(DATABASE_CREATE);
        //db.execSQL("CREATE TABLE "+Table_NAME+"("+Col_1+" INTEGER PRIMARY KEY AUTOINCREMENT,"+Col_2+" TEXT,"+Col_3+"INTEGER,"+Col_4+" TEXT,"+Col_5+" TEXT,"+Col_6+" TEXT,"+Col_7+" TEXT);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
   /* public Cursor getAll_IP()
    {
        return (getReadableDatabase().rawQuery("SELECT "+Col_1+", "+Col_2+" FROM "+Table_NAME,null));
    }*/
   public Cursor getAll_IP()
    {
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cur=db.rawQuery("SELECT "+Col_1+" as _id, "+Col_2+" from "+Table_NAME,new String [] {});

        return cur;
    }

}

和logcat窗口是

03-31 14:39:36.547 1681-1681/com.example.admin.myhome E/SQLiteLog: (1) no such table: Ip_channal_Table
03-31 14:39:36.557 1681-1681/com.example.admin.myhome E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.example.admin.myhome, PID: 1681
                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.myhome/com.example.admin.myhome.MainActivity}: android.database.sqlite.SQLiteException: no such table: Ip_channal_Table (code 1): , while compiling: SELECT ID as _id, IP_Address from Ip_channal_Table
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2348)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410)
                                                                            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4019)
                                                                            at android.app.ActivityThread.access$900(ActivityThread.java:151)
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:135)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5345)
                                                                            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:947)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
                                                                         Caused by: android.database.sqlite.SQLiteException: no such table: Ip_channal_Table (code 1): , while compiling: SELECT ID as _id, IP_Address from Ip_channal_Table
                                                                            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.rawQuery(SQLiteDatabase.java:1255)
                                                                            at com.example.admin.myhome.IP_Channal_DB.getAll_IP(NoteHolder.java:52)
                                                                            at com.example.admin.myhome.MainActivity$override.onCreate(MainActivity.java:131)
                                                                            at com.example.admin.myhome.MainActivity$override.access$dispatch(MainActivity.java)
                                                                            at com.example.admin.myhome.MainActivity.onCreate(MainActivity.java:0)
                                                                            at android.app.Activity.performCreate(Activity.java:6012)
                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410) 
                                                                            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4019) 
                                                                            at android.app.ActivityThread.access$900(ActivityThread.java:151) 
                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                            at android.os.Looper.loop(Looper.java:135) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5345) 
                                                                            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:947) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742) 

1 个答案:

答案 0 :(得分:1)

我开发了一个我在主应用程序中使用的示例项目。我希望这对你也有帮助。

<强> DataBaseHandler.java

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

import java.util.ArrayList;

public class DataBaseHandler extends SQLiteOpenHelper {

    public String BooksTable="Books";
    public String Book_Id="id";
    public String BookAuther="BookAuther";
    public String BookTitle="BookTitle";

    private  final int DB_Version=1;
    public DataBaseHandler(Context context,String DBName,int DBVersion){
        super(context, DBName,null,DBVersion);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("Create Table " + BooksTable + "(" + Book_Id + " INTEGER PRIMARY KEY, " + BookAuther + " Text, " + BookTitle + " Text);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    public void addBook(Book book){
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(Book_Id,book.getId());
        cv.put(BookAuther,book.getAuthor());
        cv.put(BookTitle,book.getTitle());

        db.insert(BooksTable, null, cv);
        db.close();
    }

    public ArrayList<Book> fetchAllBooks(){
        ArrayList<Book> booksList = new ArrayList<Book>();
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor c= db.query(BooksTable, null, null, null, null, null, null);
        c.moveToFirst();
        while (!c.isAfterLast()){
            Book book = new Book();
            book.setId(c.getInt(0));
            book.setAuthor(c.getString(1));
            book.setTitle(c.getString(2));
            booksList.add(book);
            c.moveToNext();
        }
        db.close();
        return booksList;
    }
}

<强> Book.java

public class Book {
private int id;
private String author;
private String title;

public void setId(int id) {
    this.id = id;
}

public void setAuthor(String author) {
    this.author = author;
}

public void setTitle(String title) {
    this.title = title;
}

public int getId() {
    return id;
}

public String getAuthor() {
    return author;
}

public String getTitle() {
      return title;
    }
}

<强> MainActivity.java

DataBaseHandler db=new DataBaseHandler(getApplicationContext(),"DB_Name",1);
Book book=new Book();
book.setId(1);
book.setTitle("My Title");
book.setAuthor("Name of Author");
db.addBook(book);