- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这段代码早些时候工作正常,数据库有 2017 年制作的表,但不知何故,这些表不再工作,无法在数据库中创建表并从中检索数据。 Android studio 没有抛出任何错误。过去 1 周就一直遇到这个问题。
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private EditText inputName, inputEmail, inputPassword;
private TextInputLayout inputLayoutName, inputLayoutEmail, inputLayoutPassword;
private Button btnSignUp,btnLogout;
//private static final String SITE_URL="http://182.19.41.106/src/okcl/";
private static final String SITE_URL="http://www.wetswd.in/android-data";
private static final String CHECK_URL = SITE_URL+"sign-up.php";
private static final String REGISTER_URL = SITE_URL+"android-process.php";
private static final String UNREGISTER_URL = SITE_URL+"android-process_unregister.php";
private static final String LOGOUT_URL = SITE_URL+"sign-up.php;// logout.php";
private static final String VERSION="1.05";
private static final String pin = "1234";//SN:added for flexibility
private DBHelper mydb ;
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mydb = new DBHelper(this);
//Add from XML Later
//MyReceiver reMyreceive = new MyReceiver();
//IntentFilter filter = new IntentFilter("BROADCAST_ACTION");
//registerReceiver(reMyreceive, filter);
inputLayoutName = (TextInputLayout) findViewById(R.id.input_layout_name);
inputLayoutPassword = (TextInputLayout) findViewById(R.id.input_layout_password);
inputName = (EditText) findViewById(R.id.input_name);
inputPassword = (EditText) findViewById(R.id.input_password);
btnSignUp = (Button) findViewById(R.id.btn_signup);
Boolean isServiceRunning=isMyServiceRunning(MyService.class);
if(isServiceRunning)
btnSignUp.setBackgroundColor(Color.RED);
else btnSignUp.setBackgroundColor(Color.BLUE);
btnLogout = (Button) findViewById(R.id.btn_logout);
inputName.addTextChangedListener(new MyTextWatcher(inputName));
inputPassword.addTextChangedListener(new MyTextWatcher(inputPassword));
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
submitForm(view);
}
});
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//logOut();
if (!validateName()) {
return;
}
if (!validatePassword()) {
return;
}
String macAdd = getMAC();
String dateTime = getDateTime();
String status = "out_"+inputName.getText().toString().trim();
if (isNetworkAvailable(getApplicationContext())) {
mydb.insertContact(macAdd , dateTime , "0", "0" , status , "N",pin);//inputName.getText().toString().trim(),
logOut(macAdd, inputName.getText().toString().trim(), inputPassword.getText().toString().trim(),pin);
//stopService(); is called inside logout
} else {
mydb.insertContact(macAdd , dateTime , "0", "0" , status.toUpperCase(), "N",pin);//,inputName.getText().toString().trim(),"9999"
btnLogout.setText(R.string.btn_LogOut);// "LOGOUT");
stopService(); //without Checking
Toast.makeText(getApplicationContext(), "LOG Stopped", Toast.LENGTH_LONG).show();
}
}
});
}
/**
* Validating form
*/
private void submitForm(View view) {
if (!validateName()) {
return;
}
if (!validatePassword()) {
return;
}
GPS_Slim gps = new GPS_Slim(MainActivity.this);
//gps.setUsrId(inputName.getText().toString().trim());
//gps.setPin(pin);
if (gps.canGetLocation()) {
double latitude = 20.37;//Near Park;
double longitude = 85.68;//Near Park
double acc=9999.0;//gps.getAccuracy();
String macAdd = getMAC();
String dateTime = getDateTime();
String status = "in_"+inputName.getText().toString().trim();
if(isNetworkAvailable(getApplicationContext())){
mydb.insertContact(macAdd , dateTime , String.valueOf(latitude), String.valueOf(longitude) , status , "N",pin);//,,String.valueOf(acc)
CheckUser(macAdd ,inputName.getText().toString().trim(),inputPassword.getText().toString().trim(),pin);
}else {
mydb.insertContact(macAdd , dateTime , String.valueOf(latitude), String.valueOf(longitude) , status.toUpperCase(), "N",pin);//,inputName.getText().toString().trim(),String.valueOf(acc)
Toast.makeText(getApplicationContext(), "Stored Internally", Toast.LENGTH_LONG).show();
btnLogout.setText(R.string.btn_LogOut+" #"+inputName.getText().toString().trim());
startService(); //without Checking//SN:Can I pass Pin here
Toast.makeText(getApplicationContext(), "LOG Started", Toast.LENGTH_LONG).show();
}
} else {
gps.showSettingsAlert();
}
//gps.locationManager.removeUpdates();//SN:Check if used?? GPS object is local here
//gps = null;
}
private boolean validateName() {
if (inputName.getText().toString().trim().isEmpty()) {
inputLayoutName.setError(getString(R.string.err_msg_name));
requestFocus(inputName);
return false;
} else {
inputLayoutName.setErrorEnabled(false);
}
return true;
}
private boolean validatePassword() {
if (inputPassword.getText().toString().trim().isEmpty()) {
inputLayoutPassword.setError(getString(R.string.err_msg_password));
requestFocus(inputPassword);
return false;
} else {
inputLayoutPassword.setErrorEnabled(false);
}
return true;
}
private void requestFocus(View view) {
if (view.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
private class MyTextWatcher implements TextWatcher {
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
public void afterTextChanged(Editable editable) {
switch (view.getId()) {
case R.id.input_name: validateName(); break;
case R.id.input_password: validatePassword(); break;
}
}
}
private boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
private String getMAC(){
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
return wInfo.getMacAddress();
}
private String getDateTime(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
private void register(String mac, String dateT, String lat, String lon,String pin,String usr,String ver) {
class RegisterUser extends AsyncTask<String, Void, String> {
ProgressDialog loading;
RegisterUserClass ruc = new RegisterUserClass();
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(MainActivity.this, "Please Wait",null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<>();
data.put("macid",params[0]);
data.put("datetime",params[1]);
data.put("lat",params[2]);
data.put("long",params[3]);
data.put("pin",params[4]);//SN:added for Flexibility
data.put("usr",params[5]);//SN:added for Flexibility
data.put("ver",params[6]);//SN:added for Flexibility
return ruc.sendPostRequest(REGISTER_URL,data);
}
}
RegisterUser ru = new RegisterUser();
ru.execute(mac,dateT,lat,lon,pin,usr,ver);
}
private void CheckUser(String mac_id,final String user_id,String password,final String pin) {
class CheckUser extends AsyncTask<String, Void, String> {
ProgressDialog loading;
RegisterUserClass ruc = new RegisterUserClass();
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(MainActivity.this, "Please Wait",null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(),"Received:"+s,Toast.LENGTH_SHORT).show();
if(s.equalsIgnoreCase("Unauthorised User")){
Toast.makeText(getApplicationContext(),"Login Fail",Toast.LENGTH_LONG).show();
}else{ //Sucessfully Login
double latitude = 20.37;//Near Park;
double longitude = 85.68;//Near Park
String macAdd = getMAC();
String dateTime = getDateTime();
register(macAdd , dateTime , String.valueOf(latitude), String.valueOf(longitude),pin,user_id,VERSION);
Toast.makeText(getApplicationContext(), "Login Pass", Toast.LENGTH_LONG).show();
startService();
Toast.makeText(getApplicationContext(), "LOG Started", Toast.LENGTH_LONG).show();
btnLogout.setText(btnLogout.getText().toString()+" *"+inputName.getText().toString().trim());
}
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<>();
data.put("macid",params[0]);
data.put("userid",params[1]);
data.put("password",params[2]);
data.put("pin",params[3]);
return ruc.sendPostRequest(CHECK_URL,data);
}
}
CheckUser ru = new CheckUser();
ru.execute(mac_id,user_id,password,pin);
}
private void logOut(String mac_id,final String user_id,String password,final String pin) {
class LogOUt extends AsyncTask<String, Void, String> {
ProgressDialog loading;
RegisterUserClass ruc = new RegisterUserClass();
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(MainActivity.this, "Please Wait",null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(),"Received:"+s,Toast.LENGTH_SHORT).show();
if(s.equalsIgnoreCase("Unauthorised User")){
Toast.makeText(getApplicationContext(),"Login Fail",Toast.LENGTH_LONG).show();
}else{ //Sucessfully Login
double latitude = 20.37;//Near Park;20.37 85.68
double longitude = 85.68;//Near Park
String macAdd = getMAC();
String dateTime = getDateTime();
register(macAdd , dateTime ,String.valueOf(latitude), String.valueOf(longitude),pin,user_id,VERSION);
Toast.makeText(getApplicationContext(), "Login Pass", Toast.LENGTH_LONG).show();
//Set Active=0 in android_data for macAdd
stopService();
HashMap<String, String> data = new HashMap<>();
data.put("macid",macAdd);
data.put("pin",pin);//SN:added for Flexibility
String result = ruc.sendPostRequest(UNREGISTER_URL,data);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(), "LOG Stopped", Toast.LENGTH_LONG).show();//20170821:Sn Testing
btnLogout.setText(R.string.btn_LogOut);//"LOGOUT");
}
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<>();
data.put("macid",params[0]);
data.put("userid",params[1]);
data.put("password",params[2]);
data.put("pin",params[3]);
return ruc.sendPostRequest(LOGOUT_URL,data);
}
}
LogOUt ru = new LogOUt();
ru.execute(mac_id,user_id, password,pin);
}
private void doUpdate(String s) {
}
// Method to start the service
public void startService() {Intent i=new Intent(getBaseContext(), MyService.class);
i.putExtra("user",inputName.getText().toString().trim());
i.putExtra("SITE_URL",SITE_URL);
i.putExtra("pin",pin);
i.putExtra("mac",getMAC());
i.putExtra("ver",VERSION);
i.putExtra("REGISTER_URL",REGISTER_URL);
startService(i);
Intent iActivity=new Intent(getBaseContext(), logOut.class);
iActivity.putExtra("user",inputName.getText().toString().trim());
startActivity(iActivity);
}
// Method to start the service
public void stopService() {
stopService(new Intent(getBaseContext(), MyService.class));
}
}
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "attendance.db";
public static final String RECORD_TABLE_NAME = "Record";
public static final String RECORD_COLUMN_ID = "id";
public static final String RECORD_COLUMN_MACID = "macid";
public static final String RECORD_COLUMN_DATETIME = "datetime";
public static final String RECORD_COLUMN_LAT = "lat";
public static final String RECORD_COLUMN_LONG = "long";
private HashMap hp;
public DBHelper(Context context) {
super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Boolean tableExist=isTableExists("Record",false,db);//SN:Not Tested Code Added from https://stackoverflow.com/questions/3058909/how-does-one-check-if-a-table-exists-in-an-android-sqlite-database
if(!tableExist) {
db.execSQL("create table Record " +
"(id integer primary key, macid text,datetime text,lat text, long text , status text , uploadstatus text, pin text)");
}
//TimeLog Added by SN to KeepTime of Last Update
Boolean timeLogExist=isTableExists("timeLog",false,db);//SN:Not Tested Code Added from https://stackoverflow.com/questions/3058909/how-does-one-check-if-a-table-exists-in-an-android-sqlite-database
if(!timeLogExist) {
db.execSQL("create table timeLog " + "(id integer primary key, macid text,datetime Long,comment text)");//??SN: Long
//insertTimeLog(0L,"MSG_UPDATE","macid");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS Record");
//db.execSQL("DROP TABLE IF EXISTS timeLog");
onCreate(db);
}
public boolean insertContact (String macid, String datetime, String lat, String longi , String status, String uploadstatus,String pin) { //,String usr,String acc- removed
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
//macId,dateTime, String.valueOf(getLatitude()),String.valueOf(getLongitude()),"way","N",pin,usrId,accText
contentValues.put("macid", macid);
contentValues.put("datetime", datetime);
contentValues.put("lat", lat);
contentValues.put("long", longi);
contentValues.put("status", status);
contentValues.put("uploadstatus", uploadstatus);
contentValues.put("pin", pin);
db.insert("Record", null, contentValues);
return true;
}
public boolean updateContactNotUsed (Integer id, String macid, String datetime, String lat, String longi, String status, String uploadstatus) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("macid", macid);
contentValues.put("datetime", datetime);
contentValues.put("lat", lat);
contentValues.put("long", longi);
contentValues.put("status", status);
contentValues.put("uploadstatus", uploadstatus);
db.update("Record", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
return true;
}
public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
return db.rawQuery( "select * from Record where id="+id+"", null );
}
public int numberOfRows(){
SQLiteDatabase db = this.getReadableDatabase();
return (int) DatabaseUtils.queryNumEntries(db, RECORD_TABLE_NAME);
}
public boolean updateTimeLog ( Long datetime, String key) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("datetime", datetime);
db.update("timeLog", contentValues, "comment = ? ", new String[] { key } );
return true;
}
public boolean insertTimeLog ( Long datetime, String key,String macid) { //,String usr,String acc- removed
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("datetime", datetime);
contentValues.put("comment", key);
contentValues.put("macid", macid);
db.insert("timeLog", null, contentValues);
return true;
}
public Integer deleteContact (Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete("Record", "id = ? ", new String[] { Integer.toString(id) });
}
public ArrayList<String> getAllCotacts() {
ArrayList<String> array_list = new ArrayList<>();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from Record", null );
res.moveToFirst();
while(!res.isAfterLast()){
array_list.add(res.getString(res.getColumnIndex(RECORD_TABLE_NAME)));
res.moveToNext();
}
res.close();
return array_list;
}
public boolean isTableExists(String tableName, boolean openDb,SQLiteDatabase mDatabase) {
if(openDb) {
if(mDatabase == null || !mDatabase.isOpen()) {
mDatabase = this.getReadableDatabase();
}
if(!mDatabase.isReadOnly()) {
mDatabase.close();
mDatabase = this.getReadableDatabase();
}
}
Cursor cursor = mDatabase.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+tableName+"'", null);
if(cursor!=null) {
if(cursor.getCount()>0) {
cursor.close();
return true;
}
cursor.close();
}
return false;
}
}
logcat-https://drive.google.com/file/d/1J1vhdRpP0GOQaRoCycHXWkXi_HiKPxxE/view?usp=sharing
最佳答案
尝试将数据库版本升级到2,因为您要向数据库添加新表,所以需要升级数据库版本。
public DBHelper(Context context) {
super(context, DATABASE_NAME , null, 2);
}
关于android - 无法将数据发送到数据库,不知道代码有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49850468/
我的问题是如何在 python 中创建一个简单的数据库。我的例子是: User = { 'Name' : {'Firstname', 'Lastname'}, 'Address' : {'Street
我需要创建一个与远程数据库链接的应用程序! mysql 是最好的解决方案吗? Sqlite 是唯一的本地解决方案吗? 我使用下面的方法,我想知道它是否是最好的方法! NSString *evento
给定两台 MySQL 服务器,一台本地,一台远程。两者都有一个包含表 bohica 的数据库 foobar。本地服务器定义了用户 'myadmin'@'%' 和 'myadmin'@'localhos
我有以下灵活的搜索查询 Select {vt:code},{vt:productcode},{vw:code},{vw:productcode} from {abcd AS vt JOIN wxyz
好吧,我的电脑开始运行有点缓慢,所以我重置了 Windows,保留了我的文件。因为我的大脑还没有打开,所以我忘记事先备份我的 MySQL 数据库。我仍然拥有所有原始文件,因此我实际上仍然拥有数据库,但
如何将我的 Access 数据库 (.accdb) 转换为 SQLite 数据库 (.sqlite)? 请,任何帮助将不胜感激。 最佳答案 1)如果要转换 db 的结构,则应使用任何 DB 建模工具:
系统检查发现了一些问题: 警告:?:(mysql.W002)未为数据库连接“默认”设置 MySQL 严格模式 提示:MySQL 的严格模式通过将警告升级为错误来修复 MySQL 中的许多数据完整性问题
系统检查发现了一些问题: 警告:?:(mysql.W002)未为数据库连接“默认”设置 MySQL 严格模式 提示:MySQL 的严格模式通过将警告升级为错误来修复 MySQL 中的许多数据完整性问题
我想在相同的 phonegap 应用程序中使用 android 数据库。 更多说明: 我创建了 phonegap 应用程序,但 phonegap 应用程序不支持服务,所以我们已经在 java 中为 a
Time Tracker function clock() { var mytime = new Date(); var seconds
我需要在现有项目上实现一些事件的显示。我无法更改数据库结构。 在我的 Controller 中,我(从 ajax 请求)传递了一个时间戳,并且我需要显示之前的 8 个事件。因此,如果时间戳是(转换后)
我有一个可以收集和显示各种测量值的产品(不会详细介绍)。正如人们所期望的那样,显示部分是一个数据库+建立在其之上的网站(使用 Symfony)。 但是,我们可能还会创建一个 API 来向第三方公开数据
我们将 SQL Server 从 Azure VM 迁移到 Azure SQL 数据库。 Azure VM 为 DS2_V2、2 核、7GB RAM、最大 6400 IOPS Azure SQL 数据
我正在开发一个使用 MongoDB 数据库的程序,但我想问在通过 Java 执行 SQL 时是否可以使用内部数据库进行测试,例如 H2? 最佳答案 你可以尝试使用Testcontainers Test
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 9 年前。 此问题似乎与 a specific programming problem, a sof
我正在尝试使用 MSI 身份验证(无需用户名和密码)从 Azure 机器学习服务连接 Azure SQL 数据库。 我正在尝试在 Azure 机器学习服务上建立机器学习模型,目的是我需要数据,这就是我
我在我的 MySQL 数据库中使用这个查询来查找 my_column 不为空的所有行: SELECT * FROM my_table WHERE my_column != ""; 不幸的是,许多行在
我有那个基地:http://sqlfiddle.com/#!2/e5a24/2这是 WordPress 默认模式的简写。我已经删除了该示例不需要的字段。 如您所见,我的结果是“类别 1”的两倍。我喜欢
我有一张这样的 table : mysql> select * from users; +--------+----------+------------+-----------+ | userid
我有表: CREATE TABLE IF NOT EXISTS `category` ( `id` int(11) NOT NULL, `name` varchar(255) NOT NULL
我是一名优秀的程序员,十分优秀!