[android]代码库
package vedios1com.android.com.preferenceactivity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.preference.PreferenceActivity;
/**
* Created by Administrator on 2016/12/5 0005.
*/
public class sql extends SQLiteOpenHelper {
public sql(Context context) {
super(context, "db", null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE user(" +
"name TEXT DEFAULT \"\"," +
"sex TEXT DEFAULT \"\")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
package vedios1com.android.com.preferenceactivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sql sq=new sql(this);
// 写入数据
// SQLiteDatabase sqLiteDatabase=sq.getWritableDatabase();
// ContentValues cv=new ContentValues();
// cv.put("name","张");
// cv.put("sex","17");
// sqLiteDatabase.insert("user",null,cv);
// cv.put("name","li");
// cv.put("sex","16");
// sqLiteDatabase.insert("user",null,cv);
// sqLiteDatabase.close();
// 读取数据
SQLiteDatabase dbread=sq.getReadableDatabase();
Cursor c=dbread.query("user",null,null,null,null,null,null);//不指定任何条件
while (c.moveToNext()){
String name=c.getString(c.getColumnIndex("name"));
String age=c.getString(c.getColumnIndex("sex"));
Toast.makeText(getApplicationContext(),name+age,Toast.LENGTH_LONG).show();
}
}
}
by: 发表于:2017-09-29 14:54:53 顶(0) | 踩(0) 回复
??
回复评论