<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                發現之前寫了這么多都沒有涉及數據庫的內容,但是數據庫又是灰常的重要,所以這里就寫一下安卓利用SQLiteOpenHelper進行數據庫操作了。 數據庫操作一般的思路是用一個類繼承SQLiteOpenHelper,然后就可以各種增刪改查了。必須要注意的是SQLiteDatabase和Cursor不用要close掉,不然會內存泄露。 提一下的是,設A繼承了SQLiteOpenHelper,那么A的onCreate函數只有在數據庫第一次創建的時候才會調用。 下面這個也不算是應用吧,只是輸一句話進數據庫然后顯示出最近的兩句而已。下面是代碼: ~~~ package com.example.mytest; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText ed; private TextView tv1; private TextView tv2; private Button bt; private SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed = (EditText) findViewById(R.id.ed); tv1 = (TextView) findViewById(R.id.tv1); tv2 = (TextView) findViewById(R.id.tv2); bt = (Button) findViewById(R.id.bt); MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this); db = helper.getWritableDatabase(); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String t = ed.getText().toString(); db.execSQL("insert into text (str) values (?)", new Object[]{t}); Cursor cursor = db.rawQuery("select * from text", null); if (cursor.moveToLast()) { String ans = cursor.getString(1); tv1.setText(ans); } if (cursor.moveToPrevious()) { String ans = cursor.getString(1); tv2.setText(ans); } cursor.close(); } }); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); db.close(); } } ~~~ ~~~ package com.example.mytest; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MySQLiteOpenHelper extends SQLiteOpenHelper{ public MySQLiteOpenHelper(Context context) { super(context, "text.db", null, 1); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL("create table text(id integer primary key autoincrement, str varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } } ~~~ ~~~ <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context=".MainActivity" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="matrix" /> <EditText android:id="@+id/ed" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" /> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="tv1" /> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv1" android:layout_marginTop="30dp" android:text="tv2" android:layout_centerHorizontal="true" /> <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv2" android:layout_marginTop="30dp" android:layout_centerHorizontal="true" android:text="保存" /> </RelativeLayout> ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看